Arranging from lowest to highest?

Reply

Join Date: Jul 2008
Posts: 23
Reputation: wussa is an unknown quantity at this point 
Solved Threads: 0
wussa wussa is offline Offline
Newbie Poster

Arranging from lowest to highest?

 
0
  #1
Aug 11th, 2008
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

  1. #include<stdio.h>
  2. #define N 10
  3. void Max(int num[],int i);
  4. void Min(int num[],int i);
  5. void sort(int num[],int i);
  6.  
  7.  
  8. int main()
  9. {
  10. int num[N],i;
  11. float sum=0,average;
  12. for(i=0;i<N;++i)
  13. {
  14. printf("please enter the number that need to be calculate");
  15. printf("\nplease make sure the number is between the 0 and 100\n");
  16. scanf("%d",&num[i]);
  17. }
  18.  
  19. for(i=0;i<N;++i)
  20. {
  21. printf("the number is %d\n",num[i]);
  22.  
  23.  
  24. }
  25. for(i=0;i<N;++i)
  26. {
  27. sum +=num[i];
  28. }
  29. printf("the total value is %.2f\n",sum);
  30. average=(sum/N);
  31. printf("And the average value is %0.2f\n",average);
  32. Max(num,i);
  33. Min(num,i);
  34. sort(num,i);
  35. printf("reorder list of numbers:\n\n");
  36. for(i=0;i<N;++i)
  37. printf("i=%d x=%d\n",i+1,num[i]);
  38. return 0;
  39.  
  40. }
  41.  
  42. void Max(int num[],int i)
  43. {
  44. int max = num[0];
  45. for(i=1;i<N;i++)
  46. {
  47. if (max<num[i])
  48. max=num[i];
  49. }
  50. printf("the value of maximum is %d\n",max);
  51.  
  52. return;
  53. }
  54.  
  55. void Min(int num[],int i)
  56. {
  57. int min=num[0];
  58. for(i=1;i<N;i++)
  59. {
  60. if (min>num[i])
  61. min=num[i];
  62. }
  63. printf("the value of minimum is %d\n",min);
  64. return;
  65. }
  66.  
  67. void sort(int num[],int i)
  68. {
  69.  
  70. int k,temp;
  71. for(k=0;k<N;k++)
  72. {
  73. if(num[k]<num[k+1])
  74. {
  75. temp=num[k+1];
  76. num[k+1]=num[k];
  77. num[k]=temp;
  78. }
  79.  
  80. return;
  81. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Arranging from lowest to highest?

 
0
  #2
Aug 11th, 2008
>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.
Last edited by Aia; Aug 11th, 2008 at 11:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: xitrum69 is an unknown quantity at this point 
Solved Threads: 3
xitrum69 xitrum69 is offline Offline
Newbie Poster

Re: Arranging from lowest to highest?

 
0
  #3
Aug 11th, 2008
Yes, you missed the closing bracket of the function sort and wrong in sort algorithm. Here is your function
  1. void sort(int num[],int i)
  2. {
  3. int k,k2,temp;
  4. for (int k=0; k<N-1; k++)
  5. for (int k2=k; k2<N; k2++)
  6. if (num[k] > num[k2])
  7. {
  8. temp = num[k];
  9. num[k] = num[k2];
  10. num[k2] = temp;
  11. }
  12. return ;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Arranging from lowest to highest?

 
0
  #4
Aug 12th, 2008
Originally Posted by xitrum69 View Post
[...] Here is your function
  1. void sort(int num[],int i)
  2. {
  3. int k,k2,temp;
  4. for (int k=0; k<N-1; k++)
  5. for (int k2=k; k2<N; k2++)
  6. if (num[k] > num[k2])
  7. {
  8. temp = num[k];
  9. num[k] = num[k2];
  10. num[k2] = temp;
  11. }
  12. return ;
  13. }
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 int type inside of a for loop.
Last edited by Aia; Aug 12th, 2008 at 12:05 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: xitrum69 is an unknown quantity at this point 
Solved Threads: 3
xitrum69 xitrum69 is offline Offline
Newbie Poster

Re: Arranging from lowest to highest?

 
0
  #5
Aug 12th, 2008
Originally Posted by Aia View Post
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.
Exactly, the "i" paramter is not used in this case.
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:
  1. void sort(int num[],int i)
  2. {
  3. int k,k2,temp;
  4. for (k=0; k<N-1; k++)
  5. for (k2=k; k2<N; k2++)
  6. if (num[k] > num[k2])
  7. {
  8. temp = num[k];
  9. num[k] = num[k2];
  10. num[k2] = temp;
  11. }
  12. return ;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Arranging from lowest to highest?

 
0
  #6
Aug 12th, 2008
>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 ?
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 23
Reputation: wussa is an unknown quantity at this point 
Solved Threads: 0
wussa wussa is offline Offline
Newbie Poster

Re: Arranging from lowest to highest?

 
0
  #7
Aug 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: xitrum69 is an unknown quantity at this point 
Solved Threads: 3
xitrum69 xitrum69 is offline Offline
Newbie Poster

Re: Arranging from lowest to highest?

 
-1
  #8
Aug 12th, 2008
Originally Posted by wussa View Post
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.
Just copy and paste. I tested. It ran good.
  1. void sort(int num[],int i)
  2. {
  3. int k,k2,temp;
  4. for (k=0; k<N-1; k++)
  5. for (k2=k+1; k2<N; k2++)
  6. if (num[k] > num[k2])
  7. {
  8. temp = num[k];
  9. num[k] = num[k2];
  10. num[k2] = temp;
  11. }
  12. return ;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Arranging from lowest to highest?

 
0
  #9
Aug 12th, 2008
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*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Arranging from lowest to highest?

 
0
  #10
Aug 13th, 2008
Originally Posted by iamthwee View Post
What is the point of the end return? etc etc.
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC