0

Trying to print out the user input of the array online but ended up in prints in one element at a time.

The following code aims to compute the sum of elements in the n number of ArrayList:

// let int count be counter
int count=0;
int inputNum;

// calculate the length of the array
int len;

for (int i=0; i<numOfLines; i++)
{
    count++;
    printf("Enter line %d: \n", count);

    for (int j=0; j<numOfLines; j++)
    {
        scanf("%d", &inputNum);
        printf("DEBUG:input number %d \n", inputNum++);
        if (inputNum != 0)
        {
            int arrNum[]= {inputNum++};
            len = sizeof(arrNum)/sizeof(arrNum[0]);
            printf("Total: %d \n", len);
        }
    }

}

Output:

Enter number of lines:
2

Enter line 1:

3 2 3 4

DEBUG:input number 3

Total: 1

DEBUG:input number 2

Total: 1

Enter line 2:

DEBUG:input number 3

Total: 1

DEBUG:input number 4

Total: 1

Correct sample output:

Enter number of lines:

2

Enter line 1:

3 2 3 4

Total: 9

Enter line 2:

4 1 2 3 4

Total: 10

1 Answer 1

2

It seems the first number of each line is the number of data in that line. Use that information.

// let int count be counter
int count=0;
int inputNum;

for (int i=0; i<numOfLines; i++)
{
    count++;
    printf("Enter line %d: \n", count);

    int sum = 0;
    scanf("%d", &inputNum);
    for (int j=0; j<inputNum; j++)
    {
        int value;
        scanf("%d", &value);
        printf("DEBUG:input number %d \n", value);
        sum += value;
    }

    if (inputNum != 0)
    {
        printf("Total: %d \n", sum);
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.