| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Senior Member | Help My Homework & I'll give you a .in domain! 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 Code: #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;
}
__________________ The Best Check Page Rank | ProTalk ShoutBox SBox Upload ImageSERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM |
| |
| | #3 (permalink) |
| NamePros Member | Your sort algorithm is flawed. Consider an array with 4 elements (just for simplification) [2,5,3,6] 0 - [5, 2, 3, 6] 1 - [5, 3, 2, 6] 2 - [5, 3, 6, 2] Wait... 3 is still less than 4. But num[3 + 1] doesn't exist! Oh noe! Plus, there is the fact that it still leaves them out of order. O(n) is a wonderful outcome for a sorting algorithm. Perhaps even the Holy Grail of Computer Science. However, this is not the way to do it ![]() Lets settle for Bubble Sort, O(n^2)! Code: int[] sort(int[] nums) {
int tmp, i, j;
for (i=0; i<N-1; i++) {
for (j=0; j<N-1-i; j++) {
if (nums[j+1] < nums[j]) {
tmp = nums[j+1];
nums[j+1] = nums[j];
nums[j] = tmp;
}
}
}
return nums;
}
Also, like I say below, I'm not a C guy. However, I'm pretty sure you need to return the array (nums), then set the original array to the sorted one, then print that. Bruce P.S. I don't actually know C, I'm a Java guy. But this is definitely flawed. There might be more errors though. Last edited by Bruce_KD; 08-11-2008 at 08:20 PM. |
| |
| | #4 (permalink) |
| Senior Member | Thanks Bruce @ Java lover, but I still don't get it Produce a running code please anyone, mwzd is waiting to push the domain to you now(well I bought it from him)![]() oh yeah, I forgot to add closing } so there is no error now but still the arranging is off!
__________________ The Best Check Page Rank | ProTalk ShoutBox SBox Upload ImageSERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM |
| |
| | #5 (permalink) |
| NamePros Member | Here's a working code, post if you have any questions. Code: #include<stdio.h>
#define N 10
int main(int argc, char *argv[])
{
int Max(int num[]);
int Min(int num[],int i);
void sort(int num[]);
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]);
printf("\nyou have entered %d/%d\n" , i+1,N);
}
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);
printf("the value of maximum is %d\n",Max(num));
printf("the value of minimum is %d\n",Min(num,N));
sort(num);
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;
}
int Max(int num[])
{
int max = num[0];
int i;
for(i=1;i<N;i++)
{
if (max<num[i])
max=num[i];
}
return max;
}
int Min(int num[],int k)
{
int min=num[N-k];
int i;
for(i=N-k+1;i<N;i++)
{
if (min>num[i])
min=num[i];
}
return min;
}
void sort(int num[])
{
int k,i,temp;
for(k=0;k<N;k++)
{
for(i=k;i<N;i++)
{
if(num[i]==Min(num,N-k))
{
temp=num[k];
num[k]=num[i];
num[i] = temp;
break;
}
}
}
return;
}
|
| |
| | #6 (permalink) |
| Senior Member | ok. I'll check it later as I am in class rite now. PM me ur directi also.thanks ok. thanks. please send me your directi username and email. cheers
__________________ The Best Check Page Rank | ProTalk ShoutBox SBox Upload ImageSERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |