Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Print sum of N integer numbers.

Input

The first line contains integer 1 ≤ T ≤ 100, denoting the number of test cases. Each case start with a number N, and the second line contains N integers separated by one space character.

Output

T lines, in each line, print sum of input numbers.

Sample Input
C#
2
5
54 78 0 4 9
3
1 2 3


Sample Output
C#
145
6


I want make printf all answer at the end..
this what I tried :
C#
#include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[20];
    do {
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100){
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);

    for (test=0; test<T ; test++){
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        scanf("%d",&array[i++]);

        int m=0, sum=0;
        for (m=0; m<N; m++)
        {
            sum = sum + array[m];
            sum = sum[j]
        }
    for (x=0; x<T; x++)
    printf ("%d\n", sum[j]);

        }
    return 0;
    }
Posted
Comments
Frankie-C 14-May-15 10:54am    
And the problem is?
While you explain better to us what the problem is take note of:
- Undefined 'j' and 'x' variables.
- Missing ';' at end of line: sum = sum[j]
- sum redefined locally on line: int m=0, sum=0;

hi,
code is mostly correct.
It seems the problem is in this part :
C++
for (m=0; m<N; m++)//calling this loop1
{
    sum = sum + array[m];
    sum = sum[j]
}

for (x=0; x<T; x++)//calling this loop2
    printf ("%d\n", sum[j]);

first of all i do not see j defined anywhere.
suggestion ::

C++
#include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[20];
	//declare another variable to store sum temporarily
	int tempSum;
    do
	{
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100){
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);
 
    for (test=0; test<T ; test++)
	{
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        scanf("%d",&array[i++]);
 
        int m=0, tempSum=0;
        for (m=0; m<N; m++)
        {
            tempSum = tempSum + array[m];
        }
		sum[test] = tempSum;
	}	
		for (x=0; x<T; x++)
		printf ("%d\n", sum[x]);
 
     return 0;
    }

did not test this by executing so please test yourself.
hope it works !!
 
Share this answer
 
v5
This is the correct code:
C++
 #include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[100];
    do
	{
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100)
		{
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);
 
    for (test=0; test<T ; test++)
	{
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        	scanf("%d",&array[i++]);
 
        int m=0;
		sum[test]=0;
        for (m=0; m<N; m++)
            sum[test] += array[m]; 
    }

	int x;
    for (x=0; x<T; x++)
    	printf ("%d\n", sum[x]);

    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900