scanf("%d", &N);
for( i = 0; i < N; i++ ) {
scanf("%d", &a);
}
printf("%d", a);
If N is bigger than 1, how do I add up the values inputed in each of the loop iterations?
for example.. If N is 2, the scanf will scan twice, and I want the 2 "a" I inputed to add up
int sum = 0;
scanf("%d", &N);
for(i=0;i<N;i++){
scanf("%d", &a);
sum = sum + a;
}
printf("%d", sum);
Now you have a sum variable with 0 value. In your for loop, you go on adding to last value of sum with a and at the end you print sum value.
Create a new variable and add a from the loop so that each inputted value is added up:
int sum=0;
scanf("%d", &N);
for(i=0;i<N;i++){
scanf("%d", &a);
sum+=a; //this is the short for sum=sum+a;
}
printf("The Sum is %d", sum);
Related
I am creating a program that manipulates a matrix.
Part of the program is that I need to generate a matrix with random inputs.
However, upon generating the matrix and printing each value of the matrix to double-check that the randomized numbers are being stored properly, the matrix seems to only be storing the last row of numbers, and then duplicating it.
Here is a screenshot to explain what I am referring to:
You can see that it creates the matrix [[3,6][7,5]] But it only shows it sores as [[7,5][7,5]]
And here is my code that isolates the problem:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, m, n, row, col;
int sum = 0, row_i=0, col_i=0;
int matrix[m][n];
int row_m[m];
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
matrix[i][j] = rand()%10;
printf("%d ",matrix[i][j]);
}
printf("\n");
}
printf("%d\n", matrix[0][0]);
printf("%d\n", matrix[0][1]);
printf("%d\n", matrix[1][0]);
printf("%d\n", matrix[1][1]);
return 0;
}
Instead of
int matrix[m][n];
int row_m[m];
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
use
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
int matrix[m][n];
int row_m[m];
to define your matrix and row_m after you got the values of m, n.
How do I write a program that reads and stores a series of integer, first ask for N,then read the value into an array, and calculate the sum of the first N values?
void main() {
int N,i,sum=0,temp;
printf("Please enter value of N\n");
scanf_s("%d", &N);
int *arr = (int*)malloc(sizeof(int)*N);
for (i = 0; i < N; i++) {
printf("Please %d value of array \n",i+1);
scanf_s("%d", &temp);
arr[i] = temp;
sum += temp;
}
printf("Sum Of N Elemetes In Array Is %d", sum);
}
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;
...
I've written this so far:
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
but when I enter 8, for example, it won't allow me to add any other numbers.
What's the problem?
while (i <= n); --> while (i <= n). Drop the ;. With the;, the while() loop never ends and { scanf("\n%d", &a); ... is never entered.
Suggest using auto formatting - easy to catch problems like this.
Also, to read n values use < #BLUEPIXY
// while (i <= n)
while (i < n)
#Shabnam You can use this code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
I am trying to calculate a gradient using several X and Y values. My problem is that after my code takes the mean of the X and Y values I cannot re-use the user-inputted X and Y values to further calculate a gradient as I don't know how to name them. I've included my code below, and I think I have to use either Arrays or Global variables for this task.
Thank you in advance, my coding knowledge is limited.
#include <stdio.h>
int main () {
int n, i;
float num[1000], total=0, mean;
printf("Enter the amount of x-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input x-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean=total/n;
printf("The mean of all the x-values entered is %.2f to 2 decimal places", mean);
{
float num[1000], total=0, mean;
printf("Enter the amount of y-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d",&n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input y-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean = total / n;
printf("The mean of all the y-values entered is %.2f to 2 decimal places", mean);
return 0;
}
}