[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 08-11-2008, 04:11 PM   #1 (permalink)
Senior Member
 
Join Date: Jan 2008
Posts: 1,342
250.95 NP$ (Donate)

wussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud of


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 Image
SERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM
wussadotcom is online now  
Old 08-11-2008, 07:09 PM   #2 (permalink)
NamePros Member
 
el_paon's Avatar
 
Join Date: Apr 2006
Posts: 52
258.25 NP$ (Donate)

el_paon is an unknown quantity at this point


I'll give it a try right now.
el_paon is offline  
Old 08-11-2008, 08:12 PM   #3 (permalink)
NamePros Member
 
Join Date: Sep 2006
Posts: 87
100.00 NP$ (Donate)

Bruce_KD will become famous soon enoughBruce_KD will become famous soon enough


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;
}
Edit:
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.
Bruce_KD is offline  
Old 08-11-2008, 09:13 PM   #4 (permalink)
Senior Member
 
Join Date: Jan 2008
Posts: 1,342
250.95 NP$ (Donate)

wussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud of


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 Image
SERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM
wussadotcom is online now  
Old 08-11-2008, 10:17 PM   #5 (permalink)
NamePros Member
 
el_paon's Avatar
 
Join Date: Apr 2006
Posts: 52
258.25 NP$ (Donate)

el_paon is an unknown quantity at this point


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;

}
I have edited the Min and Sort functions.
el_paon is offline  
Old 08-11-2008, 11:59 PM   #6 (permalink)
Senior Member
 
Join Date: Jan 2008
Posts: 1,342
250.95 NP$ (Donate)

wussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud ofwussadotcom has much to be proud of


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 Image
SERP | Check SERP position ! [NEW] Free Shoutbox Laman Web IIUM
wussadotcom is online now  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 09:40 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85