This problem is bothering me for a while. Please forgive me for posting a big question. I have tried implementing simple logic to get space separated integers. This is the problem statement that was given to me.
PROBLEM STATEMENT:
The runs scored by two cricket players is passed as input. The program must print the total runs scored by the better player. The better player is the player with a higher average. It is not necessary that both the players have played/scored in the same number of matches. If both the players have the same average, then print the runs scored by the player who has the highest total runs.
Boundary Conditions:
The number of matches played for any player will not exceed 20.
If a negative value is passed as runs scored, then the program output must be
INVALIDINPUT
.
Input Format:
The first line will contain the runs scored by player one. The scores are separated by one or more spaces. The second line will contain the runs scored by player two. The scores are separated by one or more spaces.
Output Format:
The first line will contain the total runs scored by the player having the higher average.
Sample Input/Output:
Example 1:
Input:
20 30 40
50 10
Output:
90
Explanation: Both the players have same average 30. Hence the output is the highest total runs which is by player 1. (20+30+40 = 90)
Example 2:
Input:
42 -10
22 45
Output:
INVALIDINPUT
Explanation: As -10 is passed as runs scored in the input, the program must print INVALIDINPUT
Now I have asked a question regarding this before(link:How to get space separated integers as input?). Now this was my code as a result:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a[100],b[100],sum1=0,sum2=0,av1,av2,f=1;
char ch,ch1;
int i=0,j=0,n,m;
//Two do-while loops to get the input
do{
scanf("%d%c",&a[i++],&ch);
}while(ch!='\n');n = i;
do{
scanf("%d%c",&b[j++],&ch1);
}while(ch1!='\n');m = j;
//sum1 and sum2 are the total scores of the 2 players
for(i=0;i<n;i++) sum1 += a[i];
for(j=0;j<m;j++) sum2 += b[j];
//av1 av2 are average of the two players
av1 = sum1/n; av2 = sum2/m;
//This part of the code for calculation and printing the output
//To check wether the given element is negative
for(i=0;i<n;i++) {if(a[i]<0) {f = 0;break;}}
for(j=0;j<m;j++) {if(b[j]<0) {f = 0;break;}}
if(f==1){
if(av1>av2) printf("%d",sum1);
else if(av2>av1) printf("%d",sum2);
else {
if(sum2>sum1) printf("%d",sum2);
else printf("%d",sum1);
}
}
else printf("INVALIDINPUT");
return 0;
}
Now the problem is I am getting output when I execute the above code in an online compiler but the same code fails in the compiler where I submit the assignment. I get the following message:
Your program has exceeded the time limit allocated. Please check for infinite loops or other similar scenario in your code:
I want to know how can I correct this. The two do while loops are fine for getting the input as specified in the problem statement. Is there any alternative approach — a way that I can get the two lines of integers as two strings and convert them to integer by parsing through them character by character?