0

I want to take integers like that

1 2 3

4 5 6

Than I should sum them with same line like

1+4=5

2+5=7

3+6=9

And I should print 5 7 9. To do this I tried to take values from user in this way but only first scanf is running and the other is not.What is the reason for that and how can I fix it or is there any other useful way to do that?(How many value will user enter is unknown.)

char s1[21];
scanf("%[^\n]", s1);
char s2[21];
scanf("%[^\n]", s2);
for(int i =0; i<b1; i++)
        printf("%d ",s1[i]+s2[i]);
  • `s1[i]` is the character code of the digit, not the digit value. – Barmar Dec 17 '20 at 17:00
  • You're not skipping over the spaces. Also, do you have to handle numbers with more than one digit? – Barmar Dec 17 '20 at 17:01
  • Why are you reading into a `char` array rather than `int` array? – Barmar Dec 17 '20 at 17:01
  • Because I should also check whether these lines have same amount of number or not.If they are same,I will sum them but if they're not I should print invalid.So I take them as char ruther than int @Barmar – asuman_sare Dec 17 '20 at 17:04

3 Answers3

0

You should read into integer arrays, not strings. scanf() will skip over the whitespace between the numbers.

int numbers1[b1], numbers2[b1];

for (int i = 0; i < b1; i++) {
    scanf("%d", &numbers1[i]);
}
for (int i = 0; i < b1; i++) {
    scanf("%d", &numbers2[i]);
}

for (int i = 0; i < b1; i++) {
    printf("%d ", numbers1[i] + number2[i]);
}
printf("\n");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What is b1 in size of array :) This code didn't work because we don't know b1 or b2.My problem is with that.Also to prevent skip over the whitespace I used %[^\n] as format specifier. – asuman_sare Dec 17 '20 at 17:15
  • I got that from the code you posted, I assumed it was the number of items on each line. – Barmar Dec 17 '20 at 17:16
  • Actually I said "How many value will user enter is unknown." in my question.So I need a solution. – asuman_sare Dec 17 '20 at 17:23
  • 1
    Then you need to read the whole line into a string, and use something like `strtok()` to parse it into individual numbers. See https://stackoverflow.com/questions/4513316/split-string-in-c-every-white-space – Barmar Dec 17 '20 at 17:26
0

The following program doesn't work. I should debug it, but I am too lazy for that. However take a look. It might help giving you some idea.

#include <stdio.h>

#define SIZE 50

int main() {

    int n[SIZE][SIZE], row, col, sum, i, j;
    int numCol; // column of first row
    int yeah = 1; // tells wether all the rows have the same number of columns
    char c = 'c'; // to check for the newline
    
    for(row = 0; row < SIZE; row++) {
    
        for(col = 0; c != '\n' && col < SIZE; col++) {
                
            if( scanf("%d%c", &n[row][col], &c) < 2 && row > 0 ) {
            
                if( col != numCol - 1) {
            
                    yeah = 0;
                }
                
                goto outside_pls; // the only way to exit the outer loop;
            }
        } // end inner for

        if( row == 0 ) {
            
            numCol = col;
        }
        else if( col != numCol ) {
            
            yeah = 0;
        }
    } // end outer for
    
outside_pls:
    
    if( yeah ) {
    
        printf("%d %d\n", row, col);

        for(j = 0; j < col; j++) {

            sum = 0;
    
            for(i = 0; i < row; i++) {
        
                sum += n[i][j];
            }
    
            printf("%d ", sum);
        }
    }
    else {
        
        puts("Number of columns doesn't match");
    }

    return 0;
}
anotherOne
  • 1,513
  • 11
  • 20
-2

You should take the index you are in and get all of your variables in the same index in all of your arrays.

Code should look like this

    for(int i = 0; i< array1.length(); i++){
        sum = array1[i]+array2[i]+.......;

    }
batuhand
  • 475
  • 3
  • 11