CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Feb 2012
Posts
6

## plz help me...

hello i'm student learn C programming..anyone cn help me with this question for sorting

Write a C program using a for loop that sums the even-numbered elements (elements 0, 2, and 4)
from an array list. Example for the list shown in exercise 1, the sum would be 126 (30+51+45).
need with sorting...

2. ## Re: plz help me...

Originally Posted by nur_sha
Example for the list shown in exercise 1, the sum would be 126 (30+51+45).
What is the list?

This seems to be an exercise of for() loops. First to sort the array, then to add every other number.

Hints: i += 2 for the for(), and look up bubblesort for the sort.

-Erik

3. Junior Member
Join Date
Feb 2012
Posts
6

## Re: plz help me...

this array list.
ArrayList
List[0] list[1] list[2] list[3] list[4] list[5]
30 12 51 17 45 62

yes based on bubble sort

4. ## Re: plz help me...

Okay, looks like a standard array. For your program, do the bubblesort like I hope is in your textbook; then you will have:

12 17 30 45 51 62

Do a for() loop, adding every other one.

Ex:
Code:
```int iTotal = 0, i;
for(i = 0; i < 6; i += 2)
iTotal += list[i];

printf("Total = %d\n", iTotal);```

5. Junior Member
Join Date
Feb 2012
Posts
6

## Re: plz help me...

can i know is it supposed like this code for array??
#include<stdio.h>
void main(void)
{
int list[6] = {30,12,51,17,45,62},line[6];
int Total = 0, i;

for(i = 0; i < 6; i += 2)
Total += list[i];

printf("Total = %d\n", Total);
}
because i only get the total.
so can i ask how to appear
of the list???is it use sorting??

6. Junior Member
Join Date
Feb 2012
Posts
6

## Re: plz help me...

plz help me??coz i cant run this program..it has debug.
#include<stdio.h>
void sort(int[]);
int main(void)
{
int list[6] = {30,12,51,17,45,62};
int Total = 0, i;

for(i = 0; i < 6; i += 2)
Total += list[i];

{

printf("Total = &#37;d\n", Total);
printf("list[%d]:",i);
scanf("%d", &list[i]);
}

sort(list);

printf("\n List sorted in assending order:\n");

for(i = 0; i < 6; i++)
{
/*printing array element*/
printf("%d\t",list[i]);

}
return (0);
}
void sort(int list[])
{
int pivot,checker,temp;

for(pivot=0; pivot<(6-1);pivot++)
{
for(checker=(pivot+1); checker< 6; checker++)
{
if (list[checker]<list[pivot])
{

temp=list[pivot];
list[pivot]=list[checker];
list[checker]=temp;
}
}
}
}

7. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,578

## Re: plz help me...

Originally Posted by nur_sha
plz help me??coz i cant run this program..it has debug.
We use real words here, not gibberish. It really doesn't take that long to write legibly.

8. Junior Member
Join Date
Feb 2012
Posts
6

## Re: plz help me...

thank you for helping..now i understand this coding.

9. ## Re: plz help me...

Basic Bubblesort: (this should be in your book somewhere)

Code:
```int i, j;

for(i = 0; i < 6; ++i)
for(j = i+1; j < 6; ++j)
{
if( list[i] > list[j] )
{
int iTemp = list[i];
list[i] = list[j];
list[j] = iTemp;
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•