why do the sum of numbers follow the results from before? - c

printf("The number of cases: ");
scanf("%d", &t);
for(i = 1; i <= t; i++){
scanf("%d", &n);
for(j = 1; j <= n; j++){
scanf("%d", &h);
sum = sum + h;
}
printf("Case #%d: %d\n", i, sum);
}
...
i have some problem with this code to find a total of sum
when i using "for loop".
why the results of the next case follow the results from the previous one?
...

You never clear out the value of sum after the inner loop runs, so you keep adding to the sum.
Set sum to 0 before entering the inner loop.
for(i = 1; i <= t; i++){
scanf("%d", &n);
sum = 0;
for(j = 1; j <= n; j++){
scanf("%d", &h);
sum = sum + h;
}
printf("Case #%d: %d\n", i, sum);
}

Related

Troubleshooting for loop - problems with sum

Im taking a C programming beginning course, and have a problem with a for loop.
I want to make two double arrays. One that takes input from user, and one that sums the inputs.
I then want to output the two arrays. One that shows the input, and one that shows the sums og the input in every cell.
The problem comes when I try to show the sum for every "cell" in the array.
The output just becomes the same as the input.
I can solve it with:
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
printf("%lf ", sum);
but then the assignment wouldn't be solved. Hope you can educate me.
#include <stdio.h> #include <stdlib.h>
int main(void) {
int n; //numbers of cells
int i; //The numbers in the cells
printf("How many cells in the array would you like? \n");
scanf("%d", &n);
double a[n], sum=0; // declare an array and a loop variable.
double b[n], sumo=0;
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%lf ", a[i]);
}
printf("\n"); //THIS IS WHERE THE PROBLEM STARTS
// print all the numbers in the second array
for (i = 0; i < n; i++) {
b[i] = b[i] + a[i];
printf("%lf ", b[i]);
}
return 0;
}
For starters the array b is not initialized
double b[n], sumo=0;
So this statement
b[i] = b[i] + a[i];
invokes undefined behavior.
It seems what you need is the following for loop
for (i = 0; i < n; i++) {
b[i] = a[i];
if ( i != 0 ) b[i] += a[i-1];
printf("%lf ", b[i]);
}
You can calculate the sum (aka the b array) when you read the user data:
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
if (i == 0)
{
b[i] = a[i];
}
else
{
b[i] = b[i-1] + a[i];
}
}
or do it like:
scanf("%lf", &a[0]);
b[0] = a[0];
for (i = 1; i < n; i++)
{
scanf("%lf", &a[i]);
b[i] = b[i-1] + a[i];
}
or do it like:
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
b[i] = sum;
}
The last part is how I solved the problem. I found the issue that. I didn't update the "sum" for ever loop. It adds a new value in b[I] this way. :-)
double a[n], sum=0; // declare an array and a loop variable.
double b[n];
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%.2lf ", a[i]);
}
printf("\n");
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
b[i] = sum;
printf("%.2lf ", b[i]);

How to sum two arrays in another array in C?

I want to try to get two arrays and sum them to another array, but it just doens't work, and I don't know why.
My code :
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number : \n");
scanf("%d", &v1[i]);
}
for(int i = 0 ; i < 3; i++) {
printf("Type a number : \n");
scanf("%d", &v2[i]);
}
for(int i = 0 ; i < 3; i++) {
v3[i] = v1[i] + v2[i];
scanf("%d", &v3[i]);
printf("Total : %d ", &v3[i]);
}
And when I get to type all the 6 numbers, he just don't show Total, and I have to type more to go to exit screen.
Screenshot :
What do you exactly want? Summation of each element from two arrays into a new third array? That's it right?
int main(int argc, char** argv) {
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number for v1 :\t");
scanf("%d", &v1[i]);
printf("Type a number for v2 :\t");
scanf("%d", &v2[i]);
// Add here
v3[i] = v1[i] + v2[i]; // Mind you that this could lead to integer overflow.
}
printf("\nResult Arr :\n");
for(int i = 0 ; i < 3; i++)
printf("%d\n", v3[i]);
}
What is the purpose of scanf inside the third for loop?
I think remove scanf inside third for loop:
scanf("%d", &v3[i]);
Also, remove & in printf:
printf("Total : %d ", v3[i]);
Full code: This code working fine on GCC compiler.
#include <stdio.h>
int main()
{
int v1[3],v2[3],v3[3], i;
for(i = 0 ; i < 3; i++)
{
printf("Type a number : \n");
scanf("%d", &v1[i]);
}
for(i = 0 ; i < 3; i++)
{
printf("Type a number : \n");
scanf("%d", &v2[i]);
}
for(i = 0 ; i < 3; i++)
{
v3[i] = v1[i] + v2[i];
printf("Total : %d\n", v3[i]);
}
}
Delete the third scanf and you don't want the ampersand before the v3 in the printf.
First, initialize your array so you won't have a garbage.
`int v1[3] = {0,0,0}, v2[3] = {0,0,0}, v3[3] = {0,0,0};`
in adding both arrays,
for(int i = 0; i < 3; i++){
v3[i] = v1[i] + v2[i];
printf("total: %d", v3[i]);
}
If you want to store result in v3, you need to remove scanf("%d", &v3[i]); from last loop
You need to change
printf("Total : %d ", &v3[i]);//you are passing address here, where printf expects value
to
printf("Total : %d ", v3[i]);
Also you should add a space before %d in each scanf so that it takes care of enter hit from previous input
eg
scanf(" %d", &v1[i]);
It is because you added a scanf function in the last for loop which does the summation.
So if you just remove that scanf line it will work great.
Old code:
int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i]; scanf("%d", &v3[i]); printf("Total : %d ", &v3[i]); }
New code should be
int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i]; printf("Total : %d ", &v3[i]); }
That should do it.

arithmetic average of an array with range -5 to 5 in c

I want to generate the arithmetic average from an array, but only with values from a certain range (here from -5 to 5)
Is this code ok?
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 10 || n <= 0)
{
printf("Error! number should in range of (1 to 10).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
edit : I am sorry I must have missed copying the whole code in th eheat of the moment.
It is a simple question I know bu I cannot seem to get it to work. Maybe the lack of sleep is making me go insane
Your code is wrong.
And it's poorly formatted and that's the reason why you don't see why it's wrong:
Your poorly formatted code:
...
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
} // it looks as if the for loop end here, but it doesn't
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // the for loop actually ends here
The same code formatted correctly:
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // <<< the for loop ends here
Correct code (look at the comments for an explanation):
...
int nbofnumbers = 0; // number of numbers in the interval [-5,5]
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
nbofnumbers++;
sum += num[i];
}
} // for loop must end here
average = sum / nbofnumbers; // we divide by the number of numbers
// in the interval [-5,5], not by n
printf("Average = %.2f", average);
return 0;
...

Row with the biggest sum in matrix

I have a program where I enter numbers to make a matrix. Then it sums numbers in each row and prints the sum. But I need to print even the row with the biggest sum. Can someone please help me? Thanks. http://onlinemovies.pw
Here is the code:
#include <stdio.h>
int main (void)
{
static int array [10][10];
int i, j, m, n, sum = 0;
printf ("Enter the order of the matrix\n");
scanf ("%d %d", &m, &n);
printf ("Enter the co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf ("%d", &array [i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
}
You need to declare a variable and intialize it with a minimum value, if those values are always positive you can initialize it to 0:
int max = 0;
if not, you can use INT_MIN defined in <limits.h>
#include <limits.h>
...
int max = INT_MIN;
then use it in your loop:
int max = INT_MIN, imax = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf ("Sum of the %d row is = %d\n", i, sum);
if (sum > max) {
max = sum;
imax = i;
}
sum = 0;
}
printf ("Max row (%d) is = %d\n", imax, max);

How to scan in matrix and add sum the row and col

include <stdio.h>
int main(){
int number [5][5] , row=0, col=0,sumrow[5], sumcol[5];
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5,sumrow[row]=sumcol[col]=0;row++);
for(row=0;row<5;row++)
for(col=0;col<5;col++)
{
sumrow[row]=sumrow[row]+number[row][col];
sumcol[col]=sumrow[col]+number[row][col];
}
// here I want to plus all the row in sumrow and col in sumcol
printf("Row totals ");
for(row=0;row<5;row++) {
printf("%d ",sumrow[row]);
}
printf("\n Row totals ");
for(col=0;col<5;col++){
printf("%d ",sumrow[col]);
}
return 0;
}
This code is not working. I want to read the matrix and all rows and columns.
I don't know what the problem is.
If u want to add all elements of row and column or sum of all rows and column below is the code,
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
int rowSum = 0;
int colSum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
rowSum += sum;
sum = 0;
}
printf("Sum of all the row is = %d\n", rowSum);
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
colSum += sum;
sum = 0;
}
printf("Sum of all the column is = %d\n", colSum);
}
I don't know if I understood correctly, but if you want to sum all the elements of the matrix you should use this for example:
int a[5][5],n,m,i,j, soma=0;
printf("Enter value of m and n: ");
scanf("%d%d",&m,&n);
printf("\nEnter elemets of the matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
for(i=0;i<m;++i){
sum_col += i;
}
for(j=0;j<n;++j){
sum_row += ;
}
printf("\nYour sum is: %d", soma);
getch();
return 0;
PS: I also used conio library, so you should use #include<conio.h> at the beginning
But if you want to sum the numb of rows and the numb of cols and print one by one, you should use this one:
int number [5][5] , row=0, col=0,sumrow=0, sumcol=0, cont_1=0, cont_2=0;
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5;row++){
cont_1++;
for(col=0;col<5;col++)
{
sumcol += number[row][col];
}
printf("\nThe sum of Col %d is %d", cont_1, sumcol);
sumcol = 0;
}
for(col=0;col<5;col++){
cont_2++;
for(row=0;row<5;row++)
{
sumrow += number[row][col];
}
printf("\nThe sum of Row %d is %d", cont_2, sumrow);
sumrow = 0;
}
return 0;
I just made your code more clear, so you should understand what you are doing.

Resources