Sunday, July 12, 2009

I have made a c program to find maximum number, but it's not working , can some one tell me whats the wrong .

hear are my program. it is writen to enter a 5 numbers and find the maximum of it. but it's not working. can some one tell me what was the mistake i have done





#include%26lt;conio.h%26gt;


#include%26lt;stdio.h%26gt;


int max(int x, int y);


#define n 5


void main (void)


{


int c;


int num[5];


for(c=0;c%26lt;n;c++)


{


printf("\nenter number%d:",c+1);


scanf("%d",%26amp;num[c]);


}


max(num[c],num[c+1]);


printf("\n maximum number is%d:%d",c+1,num[c]);


}


int max(int x, int y)


{


int z;


z=(x%26gt;=y) ? x : y;


return(z);


}

I have made a c program to find maximum number, but it's not working , can some one tell me whats the wrong .
ur not passing the array to the function,


ur just passing two numbers


mostly it is num[0],num[1]


try using two loops


for(i=0;i%26lt;5;i++)


{


for(j=0;j%26lt;i;j++)


{


if(num[i]%26lt;num[j])


---------------------------------


work on these lines
Reply:alright here is ur problem....








ur function int max(int x, int y), is not void, and returns the value of the max between 2 numbers....so





max(num[c],num[c+1]);


printf("\n maximum number is%d:%d",c+1,num[c]);





when u call it right there, nothing is being done with the value returned.....and you need another variable





i'd do something like





#include%26lt;conio.h%26gt;


#include%26lt;stdio.h%26gt;


int max(int x, int y);


#define n 5


void main (void)


{


int c;


int num[5];


for(c=0;c%26lt;n;c++)


{


printf("\nenter number%d:",c+1);


scanf("%d",%26amp;num[c]);


}











// assume the first index is the max value


int max_val = num[0];





// i'm assuming you were trying


// to store the index where the max val was found


int max_val_index = 0;





// start i at 1 since, max_val starts with the value


// of num[0]


for(int i = 1; i %26lt; n; ++i){


max_val = max(max_val,num[i]);





if(max_val == num[i])


max_val_index = i;





}


printf("\n maximum number is%d:%d",max_val_index,max_val);





}








int max(int x, int y)


{


int z;


z=(x%26gt;=y) ? x : y;


return(z);


}


No comments:

Post a Comment