/* program to find largest and lowest value in n no. of values*/
#include
void main()
{
int i=1, n, max=-9999, min=-9999, x;
printf ("\n enter the No. of value:");
scanf ("%d", &n);
while (i<=n)
{
printf ("\n Enter the value");
scanf ("%d", &x);
if (x>min)
max=x;
else
min=x;
i=i+1;
}
printf ("\n largest is %d lowest=%d", max, min);
return;
}
2006-12-05 04:52:13
·
answer #1
·
answered by Rajiv 1
·
0⤊
1⤋
I'll be using int types here, but floats, doubles, etc should also work
--------------------------------------------------------------------------------
Let's assume these numbers are in an array of integers ("int") called "numbers":
int numbers[n];
where "n" is the size of the array. For example's sake, let's say the array has the contents:
1, 2, 3, 4, 5, 6, 7, 8, 9
The best way for this is to write a small "for" statement to iterate through the array until it reaches index n-1 (remember, arrays count from zero, so we don't want to overshoot), and we'll also need a variable for the highest number, called "largest", and it'll be a type "int":
int largest;
We'll initialise this to zero (C doesn't zero new variables):
int largest = 0;
We'll need a variable for the "for" statement, an int called "counter":
int counter = 0;
Now, we'll check each index (cell) in the array, and if the current value in the array is more than the current largest value, we make that the largest value. Here's the full code, feel free to make it into a function, etc:
//----------------begin code------------------
int numbers [n]; // remember, "n" amount of values
int largest = 0;
int counter = 0;
for(counter = 0; counter < n; counter++)
if(numbers[counter] > largest) largest = numbers[counter];
//-------------------end code-----------------
I've coded this in a relatively long way round, you can declare the variables in one line, etc, but that's up to you. Make sure you understand this program by maybe including printf lines explaining which variables have which value, etc.
Good Luck.
2006-12-05 12:43:09
·
answer #2
·
answered by DonnellyC 2
·
0⤊
0⤋
Check this out
#include
void main()
{
int i=1, n, max=0, x;
printf ("\n enter the total No. of values u want to enter:");
scanf ("%d", &n);
printf("please enter %d values to find the Largest number",n);
while (i<=n)
{
printf ("\n Enter the %d value ",i);
scanf ("%d", &x);
if(i==1)
max=x;
else if (x>max)
max=x;
i=i+1;
}
printf ("\n largest value is %d ", max);
return;
}
2006-12-06 02:20:57
·
answer #3
·
answered by mallikj2♠ 2
·
0⤊
0⤋
ok this is in c++..just change the syntax where necessary.
#include
#include
void main()
{
int n,arr[100],temp;//assuming n<100//
cin>>n;
for(int i=0;i
{
cin>>arr[i];
}
//now we can use the bubble sort technique to arrange the numbers in descending order//
for(i=0;i
{
for(int j=i+1;j
{
if (arr[j]>arr[i])
{
temp=arr[i];
arr[j]=arr[i];
arr[j]=temp;
}
}
}
cout<
}//end of void main function//
2006-12-05 12:50:24
·
answer #4
·
answered by bibhash_dash 1
·
0⤊
0⤋
#include
main(){
int n, i, a[ ];
int large=0;
printf("enter he number of numbers\n");
scanf("%d",n);
printf("Enter %d numbers",n);
for (i=0;i
scanf("%d",a[i]);
for (i=0;i
if (a[i]>a[i+1])
Large=a[i];
Printf ("The Largest is %d",Large);
}
2006-12-06 07:01:55
·
answer #5
·
answered by Ravi Nanjunda Rao 3
·
0⤊
0⤋
.
Why dont u try urself ?
**********
Vasu M
**********
2006-12-06 00:29:25
·
answer #6
·
answered by V@su Maniram 3
·
0⤊
0⤋