Write a program that asks the user to enter a list of integers. The program is to determine the largest value entered and the number of times it was entered. For example, if the following series is entered
5 2 15 3 7 15 8 9 5 2 15 3 7
it would output the largest number is 15 and it was entered 3 times. You should ask the user the number of integers he/she wants to input.
TURBO C Programming?
Try this.
#include %26lt;stdio.h%26gt;
#include %26lt;malloc.h%26gt;
main()
{
int i,no,*num,max, ctr;
printf("Enter the number integers to be input-%26gt;");
scanf("%d",%26amp;no);
//dynamic allocation due to the fact that the number of integers is variable.
//if you are unfamiliar to this you can use a large array like num[100] or something like that instead of *num in the declaration
num=(int *)malloc(no*sizeof(int));
max=0;
printf("Enter the numbers separated by spaces-%26gt;");
for (i=0; i%26lt;no; i++)
{
scanf("%d", num+i);
if (*(num+i) %26gt; max)
max=*(num+i);
}
ctr=0;
for (i=0; i%26lt;no;i++)
{
if (*(num+i)==max)
++ctr;
}
printf("Largest no. %26lt; %d %26gt; was entered %26lt; %d %26gt; times\n", max, ctr);
}
Reply:This is a part of the program.
int i, n,max,count,num = 0;
printf('How many numbers?');
scanf("%d",n);
for (i=1;i++;i%26lt;=n){
scanf("%d",num);
if (max%26lt;num) {
max=num;
count=1;
}
else if (max==num)
{ count+=1;
}
}
Reply:/* I think you are in the learning stage, so I am here give you easyly understandable code*/
#define MAX 100
main()
{
int a[Max],i,n,tempmax=0,count=1;
/*Here we read max number of array length*/
printf("enter how many nums you like to enter\n");
scanf("%d",%26amp;n);
/*Here we are reading array values*/
printf("enter array nums\n");
for(i=1; i%26lt;=n; i++)
scanf("%d", %26amp;a[i]);
/*It is time to find max number and number of reapts of that
number*/
for(i=1;i%26lt;=n;i++)
{
if(tempMax==a[i]))
{
count++;
}
else if(tempMax%26lt;a[i])
{
tempMax=a[i];
}
}
printf("Max num =%d\n",tempMax);
printf("Max num %d reapts",count);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment