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;
...
Related
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);
}
instructions are:
Write a program which reads 10 different integers from the user and finds and prints
a) Minimum element
b) Sum of all elements and
c) Average of elements.
Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a
there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
you min variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}
My array isn't printing out all the data, just whatever was last inputted. The data should print something like this
For the student number, if not enough numbers are inputted, 0's are automaticaly put in.
/*
Name:
Date: 10/06/2016
Workshop 4
*/
#include <stdio.h>
int main(void)
{
int counter;
int marks [40];
float num_grades = 0;
int row = 1;
float sum = 0;
float average = 0;
int pass = 0;
int fail = 0;
float pass_sum = 0;
float fail_sum = 0;
float pass_average = 0;
float fail_average = 0;
float biggest = 0;
float smallest = 0;
//int grade[40];
int student_num[40];
printf(" ---=== IPC mark Analyser V2.0 ===---\n");
printf("Please enter the number of students(between 3 and 40): ");
scanf("%d", &counter);
while (counter >40 || counter <3)
{
printf("Invalid number, enter a number between 3 and 40 inclusive: ");
scanf("%d", &counter);
}
printf("Row Std No Mrk\n");
printf("--- --------- ---\n");
num_grades = counter;
while (counter > 0)
{
printf("%d ", row);
printf("_____________ ___\r%3d ", row);
scanf("%d", &student_num[40]);
scanf("%d", &marks[40]);
row++;
counter--;
sum += marks[40];
}
for (int i = 0; i < num_grades; i++)
{
printf("%03d %09d %3d\n", row, student_num[40], marks[40]);
}
average = sum / num_grades;
printf("-----------------\n");
printf("-----------------\n");
printf("Marks Entered, printing results:\n");
printf("Row Std No Mrk\n");
printf("--- --------- ---\n");
printf("The average of all marks in this group is %.1f.\n", average);
printf("Program Ended.\n");
return 0;
}
You're always reading/writing index 40 in the student_num and marks arrays, so everything goes to the same place.
Actually, the valid indexes of an array of size 40 are 0 to 39, so you're actually reading/writing off the end of the array, causing undefined behavior.
You need to use the proper index in each place. In the printing loop, use i. In the reading loop, use a variable that starts at 0 goes up to counter.
num_grades = counter;
for (int i = 0; i < num_grades; i++)
{
printf("%d ", i + 1);
printf("_____________ ___\r%3d ", i + 1);
scanf("%d", &student_num[i]);
scanf("%d", &marks[i]);
sum += marks[i];
}
for (int i = 0; i < num_grades; i++)
{
printf("%03d %09d %3d\n", row, student_num[i], marks[i]);
}
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;
}
}
Sum and get the average of the values in the array, and stop to ask a number when the sum exceeds 10,000.I need to clear it with an example of these conditions.
The language fit that need the example is in C
my code
```
#include <stdio.h>
int main()
{
int array[10];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
```
Answer of your Question
#include <stdio.h>
int main()
{
int array[10];
int num, negative_sum = 0, positive_sum = 0,j;
static int i=0;
float total = 0.0, average;
label:
j=i;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i; i < (j+num); i++)
{
scanf("%d", &array[i]);
}
i=j;
printf("Input array elements \n");
for (i; i < (j+num); i++)
{
printf("%d\n", array[i]);
}
i=j;
for (i; i < (j+num); i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + (float)array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
if(total<10000)
{
printf("Total is less than 10000 and Now total is : %.2f\n",total);
goto label;
}}
it is the answer of your question.program will run when your total is exceed upto 10000.