Thursday, July 30, 2009

Hey i have got a tricky question could u help me?

acctualy i am prepring for my exams and not able to write the input of ques if u have done c programing in computers then plz tell me the syntax(input) of this ques. output i will find through "turbo c+=" so the question is "" write the programe to find largect number from the list of 10 number enter through keyboard" plz tell the syntax of it with using the while etc. loops of 9th standard plz

Hey i have got a tricky question could u help me?
#include%26lt;iostream.h%26gt;





void main()


{


int a[10],largest,i;


cout%26lt;%26lt;"Enter : ";


i=0;


while(i%26lt;10)


{


cin%26gt;%26gt;a[i];


i++;


}





i=1;


largest=a[0];


while(i%26lt;10)


{


if(largest%26lt;a[i])


{


largest=a[i];


}


i++;


}


cout%26lt;%26lt;"Largest is = "%26lt;%26lt;largest;





}





//cheers
Reply:Well, since you know you will get 10 and only 10 values, the easiest approach I can think of is this:





1. Create a double array of size 10.


2. Set up a loop that, in each iteration, asks the user for a number and stores it in the next empty array element.


3. Create a variable to store max value and set the value of the variable to the first element in the array.


4. Traverse the rest of the whole array, using a loop, and if the current array value is greater than your current maximum, set the maximum to the new value.





In psuedocode....





FindMaxValue()


..numbers[10]


..for loop_count = 0 to 9


....[output] "enter a number: "


....numbers[loop_count] = [input]


..end for





..max_value = numbers[0]


..for loop_count = 1 to 9


....if numbers[loop_count] %26gt; max_value


......max_value = numbers[loop_count]





..[output] "maximum value entered is " max_value


No comments:

Post a Comment