| | |
Arranging from lowest to highest?
![]() |
•
•
Join Date: Jul 2008
Posts: 23
Reputation:
Solved Threads: 0
This is in c programming.
The question is not with me right now but its like this, the program wants me to input 10 numbers and then find the average,total,maximum and minimum, Then print back the numbers according from lowest to highest.
I'm having a bit of difficulty doing it with no errors. Help me finish this problem and I'll give you a free domain
Locaton.in ~ gimme your directi@reseller club ID,email.
Thanks
The question is not with me right now but its like this, the program wants me to input 10 numbers and then find the average,total,maximum and minimum, Then print back the numbers according from lowest to highest.
I'm having a bit of difficulty doing it with no errors. Help me finish this problem and I'll give you a free domain
Locaton.in ~ gimme your directi@reseller club ID,email.
Thanks
C Syntax (Toggle Plain Text)
#include<stdio.h> #define N 10 void Max(int num[],int i); void Min(int num[],int i); void sort(int num[],int i); int main() { int num[N],i; float sum=0,average; for(i=0;i<N;++i) { printf("please enter the number that need to be calculate"); printf("\nplease make sure the number is between the 0 and 100\n"); scanf("%d",&num[i]); } for(i=0;i<N;++i) { printf("the number is %d\n",num[i]); } for(i=0;i<N;++i) { sum +=num[i]; } printf("the total value is %.2f\n",sum); average=(sum/N); printf("And the average value is %0.2f\n",average); Max(num,i); Min(num,i); sort(num,i); printf("reorder list of numbers:\n\n"); for(i=0;i<N;++i) printf("i=%d x=%d\n",i+1,num[i]); return 0; } void Max(int num[],int i) { int max = num[0]; for(i=1;i<N;i++) { if (max<num[i]) max=num[i]; } printf("the value of maximum is %d\n",max); return; } void Min(int num[],int i) { int min=num[0]; for(i=1;i<N;i++) { if (min>num[i]) min=num[i]; } printf("the value of minimum is %d\n",min); return; } void sort(int num[],int i) { int k,temp; for(k=0;k<N;k++) { if(num[k]<num[k+1]) { temp=num[k+1]; num[k+1]=num[k]; num[k]=temp; } return; }
>I'm having a bit of difficulty doing it with no errors.
What do you mean by "...doing it with no errors"?
You are missing the closing bracket for the function sort.
Your formatting needs some work. It is kind of hard to read that way. A guide here.
What do you mean by "...doing it with no errors"?
You are missing the closing bracket for the function sort.
Your formatting needs some work. It is kind of hard to read that way. A guide here.
Last edited by Aia; Aug 11th, 2008 at 11:25 pm.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 3
Yes, you missed the closing bracket of the function sort and wrong in sort algorithm. Here is your function
c Syntax (Toggle Plain Text)
void sort(int num[],int i) { int k,k2,temp; for (int k=0; k<N-1; k++) for (int k2=k; k2<N; k2++) if (num[k] > num[k2]) { temp = num[k]; num[k] = num[k2]; num[k2] = temp; } return ; }
•
•
•
•
[...] Here is your function
c Syntax (Toggle Plain Text)
void sort(int num[],int i) { int k,k2,temp; for (int k=0; k<N-1; k++) for (int k2=k; k2<N; k2++) if (num[k] > num[k2]) { temp = num[k]; num[k] = num[k2]; num[k2] = temp; } return ; }
Last edited by Aia; Aug 12th, 2008 at 12:05 am.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 3
•
•
•
•
Nope, that's your function, not his. What's the purpose of that int i parameter if it is not used?. And C doesn't allow the declaration of an type inside of a for loop.
My puprose is to fix the errors from Wuss to change his sort algorithm and try to unchange the sort prototype.
Due to I am using the gcc to test the code, thanks for your reminder. The following is my modification code:
c Syntax (Toggle Plain Text)
void sort(int num[],int i) { int k,k2,temp; for (k=0; k<N-1; k++) for (k2=k; k2<N; k2++) if (num[k] > num[k2]) { temp = num[k]; num[k] = num[k2]; num[k2] = temp; } return ; }
>Exactly, the "i" paramter is not used in this case.
Well, if it is doing nothing don't you think it would be correct to remove it from it?. Perhaps, it could be used as the number of members of the array, instead of using the global constant N ?
Well, if it is doing nothing don't you think it would be correct to remove it from it?. Perhaps, it could be used as the number of members of the array, instead of using the global constant N ?
for (k2=k; k2<N; k2++) That part in red wastes a cycle each time in the inner loop.for (k2 = k + 1; k2 < N; k2++) saves that extra loop. Last edited by Aia; Aug 12th, 2008 at 12:56 am.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 3
•
•
•
•
well I forgot to edit the last part. now with close } there is no error but there is still problem with agorithm to sort. Anyone can produce my code with the correct algorithm please? I have finish a lot of my time but still have not finish it.
C Syntax (Toggle Plain Text)
void sort(int num[],int i) { int k,k2,temp; for (k=0; k<N-1; k++) for (k2=k+1; k2<N; k2++) if (num[k] > num[k2]) { temp = num[k]; num[k] = num[k2]; num[k2] = temp; } return ; }
void sort ( int num[], int i )
{
int k, k2, temp;
for ( k = 0; k < N - 1; k++ )
for ( k2 = k + 1; k2 < N; k2++ )
if ( num[k] > num[k2] )
{
temp = num[k];
num[k] = num[k2];
num[k2] = temp;
}
return ;
}What is the point of the end return? etc etc.
Last edited by iamthwee; Aug 12th, 2008 at 7:23 am.
*Voted best profile in the world*
To return from the function, probably.
Just because you don't want to be explicit doesn't mean others shouldn't be. And what etc etc? I don't see that anywhere in the code.
xitrum69, you should really have braces around your loops. They make the code easier to read. It is best to be explicit, which causes fewer errors. And a cut/paste of your code doesn't help others learn. It helps them cheat. But pointing them in the right direction does help.
xitrum69, you should really have braces around your loops. They make the code easier to read. It is best to be explicit, which causes fewer errors. And a cut/paste of your code doesn't help others learn. It helps them cheat. But pointing them in the right direction does help.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Other Threads in the C Forum
- Previous Thread: Write to hex, is it possible?
- Next Thread: numerical value of EOF
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming standard strchr string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






