0

I want to write a program that gets input in separate lines (2 integers on each line) and calculates the sum of those 2 integers on each line This is my code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *numbers;
    int i, j;
    numbers = malloc(100 * sizeof(int));
    for (i = 1; i <= 100; i++)
    {
        for (j = 1; j <= 2; j++)
        {
            scanf("%d", &numbers[j]);
        }
        printf("%d\n", numbers[1]+numbers[2]);
    }


    return 0;
}

My problem is that this program gives sum right after writing 2 numbers and doesn't stop until 100 lines is reached. But I want it like this:

example input:
10 5
6 8
67 2

example output:
15
14
69

100 is just max lines an user can input. How do I do that?

OrangeBike
  • 145
  • 8
  • 1
    How do you want the program to know when the input is over? You need to define that end condition before we can suggest how to code it. e.g. End when there is a negative number, end when there is a `'q'`, have a number at the start which tells the number of lines, etc. – kaylum Apr 23 '20 at 11:20
  • This is exactly my problem! I don't know how the program should know to stop! Instruction doesn't say anything about that – OrangeBike Apr 23 '20 at 11:22
  • Check the `scanf` documentation. You're not checking the return from `scanf` which would tell you if you hit the end of file (end of user input in this case). That is, if you are ending by pressing ctrl-D (in Linux) or ctrl-Z (in Windows command line). – lurker Apr 23 '20 at 11:22
  • What instructions? If you share those we might be able to help you interpret it. We can't comment on things we can't see. There are many ways to end the input that we can tell you about. But whoever is asking you to do this may have defined some end criteria so we need to know that to be able to give more exact advice. – kaylum Apr 23 '20 at 11:22
  • Input: There is at most 100 test cases in the input in separate lines. Each test case consists of two natural numbers of at most 1000 digits each, with space between them. output: For each test cases calculate the the sum of two numbers given in a test case and print them in separate lines. – OrangeBike Apr 23 '20 at 11:25
  • this is all it says :/ – OrangeBike Apr 23 '20 at 11:26
  • @OrangeBike Is there an input on number of test cases? In the question? – Abhay Aravinda Apr 23 '20 at 11:28
  • 1
    Well if that really is all that is given then that is a badly worded problem description. You should clarify it with whoever gave you that. If you don't get it clarified, the best bet is to do what @lurker has already said. – kaylum Apr 23 '20 at 11:29
  • Thank you, I'll do that – OrangeBike Apr 23 '20 at 11:30
  • 1
    @OrangeBike if the input is from a file, you could check for endOfFile. Maybe whoever asked you to write the program wanted input from a text file – Abhay Aravinda Apr 23 '20 at 11:34
  • Maybe you're looking for this: https://stackoverflow.com/a/3764068/898348 – Jabberwocky Apr 23 '20 at 12:06

1 Answers1

1

There are two options to solve this 1) You need to have the 'n' number of inputs user is going to give 2) You should read it from the file to detect the end of line. Below code is for first option.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *numbers;
    int i, j,n;
    scanf("%d", &n);
    numbers = malloc(n * sizeof(int));

    for (i = 0; i < n; i++)
    { int s1,s2;
            scanf("%d", &s1);
            scanf("%d", &s2);
        numbers[i]= s1+s2;
    }
 for (i = 0; i < n; i++)
    { 
        printf("%d",numbers[i]);
    }

    return 0;
}
Athish Murugan
  • 331
  • 1
  • 7