|
-
February 29th, 2012, 12:04 PM
#1
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...
-
February 29th, 2012, 12:08 PM
#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
-
February 29th, 2012, 12:54 PM
#3
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
-
February 29th, 2012, 01:01 PM
#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);
-
February 29th, 2012, 01:24 PM
#5
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??
-
February 29th, 2012, 02:24 PM
#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 = %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;
}
}
}
}
-
February 29th, 2012, 02:37 PM
#7
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.
Please use code tags to format your code and keep it readable.
-
February 29th, 2012, 03:01 PM
#8
Re: plz help me...
thank you for helping..now i understand this coding.
-
February 29th, 2012, 03:27 PM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|