1

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?

Community
  • 1
  • 1
Lingesh.K
  • 57
  • 2
  • 11
  • 1
    `if(sum2>sum1) printf("%d",sum1);` --> `if(sum2>sum1) printf("%d",sum2);` – BLUEPIXY Apr 10 '17 at 17:55
  • For the most part, using `scanf()` means you lose the ability to track lines. Your code would be vulnerable to trailing blanks, for example. Use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) (or perhaps POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)) to read the line, and then [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) in a loop (see [How to use `sscanf()` in loops?](http://stackoverflow.com/questions/3975236/)) to read the values from the line you read. – Jonathan Leffler Apr 10 '17 at 17:57
  • 1
    And surely it would be sensible to detect the invalid input when the number has just been read, rather than waiting until later. It would be a good idea to check the return value from `scanf()` — if it isn't `2`, you've got problems. – Jonathan Leffler Apr 10 '17 at 18:00
  • Also what BLUEPIXY said but you need to fix the value printed in the `else` clause too — or simply switch `>` for `<`. – Jonathan Leffler Apr 10 '17 at 18:02
  • Your loops to scan numbers only terminate when a number is immediately followed by a newline... if there's anything else, even other whitespace, they continue scanning numbers from the next line. Eventually that could lead to overrunning the array bounds, or an infinite loop after the last number. Even without trailing whitespace on each line, if there's no newline after the last line you'll have a problem there. – Dmitri Apr 10 '17 at 18:03
  • I have fixed the error in the if-else loop @BLUEPIXY but still, problem lies with getting the input of spaced integers.I guess the only possible solution is to get the two lines as two strings and convert them to integer arrays but how can I do so? – Lingesh.K Apr 10 '17 at 18:16

0 Answers0