Reputation: 87
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
Upvotes: 0
Views: 137
Reputation: 75062
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);
}
}
Upvotes: 2