Maximum


This is a companion discussion topic for the original entry at https://toph.co/p/maximum
#include<stdio.h>
 short maxi(short num[],short n);
 short main()
 {
           short n;
           scanf("%d",&amp;n);
           printf("\n");
           short num[n-1]; 
           for(int i=0;i&lt;n;i++)
           {
                     scanf("%d",&amp;num[i]);
           }
           printf("%d",maxi(num,n));
 }
 short maxi(short num[],short n)
 {
             short max=num[0]; 
             for(short i=1;i&lt;n;i++)
             {
             if(max&lt;num[i]) max=num[i]; 
             }
 return max;
 }

what’s the problem with the above code?
Every timei run it it returns runtime error.

1 Like

Why is this n-1? There will be n numbers.

#include<iostream>
using namespace std;

int main()
{
	int q;
		if(q<100)
		{
			cin >>q;
		}
		
		int a,b,c;
		cin >>a >>b >>c;
		
		if(b>a && c<b)
		{
			cout <<b <<endl;
		}
		else if(a>b && a>c)
		{
			cout << a <<endl;
		}
		else
		{
			cout <<c <<endl;
		}
	
	return 0;
}

what’s the problem with the above code???

1 Like

for some reason toph says my answer is wrong
I’ve checked it with a larger set from uDebug and it matches

#include<stdio.h>

int main(){
int n,max=1;

printf("No of set: ");
scanf("%d",&n);
int arr[n];

for(int i=0; i<n; i++){
    scanf("%d",&arr[i]);
    if (arr[i]>max)
       {
         max=arr[i];
       }
}
printf("max: %d",max);

return 0;
}

@raridoy4.2018 You are reading only 3 numbers. You need to write a loop that will read all N numbers and compare them to find the maximum.

@farzeen_naz You need to print the maximum number only. You do not need to prefix it with the text "max: ".

You also should not print texts like No of set: .

#include<stdio.h>

int main()
{
	int s,q,l;
	scanf("%d",&s);
	int a[ s  ];
	for(q=0;q<s;q++){
        scanf("%d ",&a[ q ]);
	}
	a[ s ] =0;
	for(q=0;q<s;q++){
        if(a[ q ] < a[ q + 1 ]){
            l = a[ q + 1 ];
        }
	}
	printf("%d", l);
	return 0;
}

//what’s problem here?

@Anirban You are overwriting one of the input values here.

And, your second loop, at its last iteration, is comparing the last number in the array with something outside of the array.

ok, but I have another problem, that’s my program is taking 1 extra input than I told.

Can you give an example of the issue you are talking about? Like give us an input for which you get an extra number and what it is.


see here, 1st number is telling how much number will I give, but it takes one extra number

@Anirban Remove that space after %d (in "%d " in the second scanf).

1 Like

Thank you very much brother, It will be a pleasure to me if I can contact (incase I have stacked in any problem ) with you through email / fb.
Please give me your id.

#include <iostream>
using namespace std;

 int main()
 {
 	int N;
 	cin >> N;
 	cout << endl;
 	if(N>99) return 0;
 	int arr[N];
 	
 	int max = 0;
 	for(int i=0; i<N; i++)
 	{
 		cin >> arr[i];
 		if(arr[i]>max)
 			max = arr[i];
 	}
 	cout << max;
 	return 0;
 }

What is the problem in this code?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a,b,c;
cin>>n;
while(cin>>a>>b>>c)
{

cout<<max(a,max(b,c))<<endl;
break;

}
return 0;
}
what’s the problem with the above code???

When I do Build and Run, my IDE shows the correct answer, but when I test it here, it shows me Wrong Answer! I can’t just find whats wrong in this code…

#include <stdio.h> 
  
int main() 
{ 
    int A, B, C; 
  
    printf("Enter the numbers A, B and C: "); 
    scanf("%d %d %d", &A, &B, &C); 
  
    if (A >= B && A >= C) 
        printf("%d is the largest number.", A); 
  
    if (B >= A && B >= C) 
        printf("%d is the largest number.", B); 
  
    if (C >= A && C >= B) 
        printf("%d is the largest number.", C); 
  
    return 0; 
}

@iker_13 Your code assumes that there will only be 3 numbers (A, B, C). But in the problem statement, it says there will be “N numbers” (where N can be anything between 1 and 100).

What is wrong with this code? Shows and runs perfectly in my IDE, but shows compilation error in toph.co.

#include <stdio.h>
int main()
{
  int i,num,n,large=0;

  printf("How many numbers: ");
  scanf("%d",&n);

  for(i=0; i<n; i++)
  {
    printf("\nEnter number %d: ",i+1);
    scanf("%d",&num);
    if(num>large)
    large=num;
  }

  printf("\n\nThe Largest Number is %d",large);

  getch();
  return 0;
}

Don’t write stuff like "How many numbers:" and "\nEnter number %d". Online Judges (OJ) do not work this way.
Online Judges compile your code and then produce an output file against the input files which are known as test cases.
Then, If the output file produced by your code does match the Accepted output files stored in the OJ, your submission will be accepted.
Suppose you are told to read 2 integers and print their sum in a single line. The for input:

101 202

the desired output should be.

303

So, if the output of your code is

303

then the submission will be accepted.

But if the output of your code is:

The sum is 303

then your output does not match with the desired output and so the code will not be accepted.

So, in order to get your codes accepted, you have to output the result in the way which was explained in the problem statement.

Fix your code according to the problem statement to get Accepted verdict.

1 Like