How to get sum of this array? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I'm troubleshooting my code right now and I'm trying to get the sum of the first and second score. However, it results to a million value.
#include <stdio.h>
int main ()
{
int points [10], n, i, sum = 0, ave;
float max, average;
printf("Enter number of activities to compute: ");
scanf("%d", &n );
printf("Enter total points possible: ");
scanf("%f", &max);
for(i=0; i < n; ++i){
printf("Enter points earned for each activity %d: ", i+1);
scanf("%d", &points [i]);
sum += points [i];
printf("%d", &sum);
}
}
When running the code this is the result:
Enter number of activities to compute: 2
Enter total points possible: 20
Enter points earned for each activity 1: 10
6618600Enter points earned for each activity 2: 5
6618600
My goal is to make it 15.
Thanks!

You don't need to put & in printf. Adding & will print the address of the variable.
printf("%d", sum);

Related

Calculating n! and x^n and proceeding the calculation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.

Why can't I get the program to answer anything but 0.00? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes

Program outputting wrong sum [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
team1sum / team2sum is supposed to get the sum of each playerWeight1 / playerWeight2 and print it out.
My team1sum and team2sum are printing out wrong numbers.
The right output can be seen here.
#include <stdio.h>
int main() {
int i, howManyPlayers, playerWeight1, playerWeight2,
team1sum = 0, team2sum = 0;
scanf("%d", &howManyPlayers);
for (i = 0; i < howManyPlayers; i++){
scanf("%d", &playerWeight1);
scanf("%d", &playerWeight2);
team1sum = team1sum + playerWeight1;
team2sum = team2sum + playerWeight2;
}
if (team1sum > team2sum){
printf("Team 1 has an advantage\n");
printf("Total weight for team 1: %d\n", &team1sum);
printf("Total weight for team 2: %d", &team2sum);
}
else {
printf("Team 2 has an advantage\n");
printf("Total weight for team 2: %d\n", &team2sum);
printf("Total weight for team 1: ", &team1sum);
}
return 0;
}
Your printf needs the int, not the address of the int, so remove the & operator.
printf("Total weight for team 2: %d\n", team2sum);
printf("Total weight for team 1: %d", team1sum);
That should do the trick.
I ran your code and it seems that the actual sum is being calculated correctly.
By removing the & from the print statements, you should obtain the expected output.
Remember that when you print using printf, the format does matter, and printing a & to an int is not the same thing as printing an int.

scanf() is not taking more than 6 times in a for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
It is really very unexpected, I have looked a lot of time where can be the problem but couldn't find it.
Problem is:
Say I've given 7 integers to calculate their average, but it is taking first 6 numbers and calculating their average.
However I give any number more than 6, it will calculate only first 6's
The code is:
#include <stdio.h>
int main()
{
int n,i,total = 0;
int numArr[n];
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&numArr[i]);
total += numArr[i];
}
printf("--------------------------------\n");
printf("Average of this %d numbers: %d",n,total/n);
}
I'm not sure whether it is compiler problem or problem of my code.
initialize n first and then declare numArr[n]
Modify your code as
int n,i,total = 0;
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
int numArr[n];
then scan the array.

Program working fine for smaller values but throwing Segmentation Fault for bigger values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.

Resources