disclaimer: I'm new to programming
I'm working on this problem
so far ive written this which takes user inputs and calculates an average based on them
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
}
I'd like the user to enter -1 to indicate that they are done entering data; I can't figure out how to do that. so if possible can someone explain or give me an idea as to how to do it
Thank you!
#include <stdio.h>
int main()
{
int i = 0;
float num[100], sum = 0.0, average;
float x = 0.0;
while(1) {
printf("%d. Enter number: ", i+1);
scanf("%f", &x);
if(x == -1)
break;
num[i] = x;
sum += num[i];
i++;
}
average = sum / i;
printf("\n Average = %.2f", average);
return 0;
}
There is no need for the array num[] if you don't want the data to be used later.
Hope this will help.!!
You just need the average. No need to store all the entered numbers for that.
You just need the number inputs before the -1 stored in a variable, say count which is incremented upon each iteration of the loop and a variable like sum to hold the sum of all numbers entered so far.
In your program, you have not initialised n before using it. n has only garbage whose value in indeterminate.
You don't even need the average variable for that. You can just print out sum/count while printing the average.
Do
int count=0;
float num, sum = 0;
while(scanf("%f", &num)==1 && num!=-1)
{
count++;
sum += num;
}
to stop reading at -1.
There is no need to declare an array to store entered numbers. All you need is to check whether next entered number is equal to -1 and if not then to add it to the sum.
Pay attention to that according to the assignment the user has to enter integer numbers. The average can be calculated as an integer number or as a float number.
The program can look the following way
#include <stdio.h>
int main( void )
{
unsigned int n = 0;
unsigned long long int sum = 0;
printf("Enter a sequence of positive numbers (-1 - exit): ");
for (unsigned int num; scanf("%u", &num) == 1 && num != -1; )
{
++n;
sum += num;
}
if (n)
{
printf("\nAverage = %llu\n", sum / n);
}
else
{
puts("You did not eneter a number. Try next time.");
}
return 0;
}
The program output might look like
Enter a sequence of positive numbers (-1 - exit): 1 2 3 4 5 6 7 8 9 10 -1
Average = 5
If you need to calculate the average as a float number then just declare the variable sum as having the type double and use the corresponding format specifier in the printf statement to output the average.
Related
i have a homework but i cant get the answer
I need to write a program in C...
Here is what is needed: You need to enter "n" natural number as input , and from all the natural numbers smaller than "n" , its needed to print the number which has the highest sum of devisors.
For exp: INPUT 10 , OUTPUT 8
Can anyone help me somehow?
I would really appreciate it !
i tried writing a program for finding the devisor of a number but i cant get far from here
#include <stdio.h>
int main() {
int x, i;
printf("\nInput an integer: ");
scanf("%d", &x);
printf("All the divisor of %d are: ", x);
for(i = 1; i < x; i++) {
if((x%i) == 0){
printf("\n%d", i);
}
}
}
I have implemented using function which will takes input number from user and then return the sum of divisor. hope this is one you looking for
/* function to return of sum of divisor
** input: x: integer number from user input
** return sum: sum of divisor of x
*/
int sum_of_divisor(int x)
{
int sum = 0;
for(int i = 1; i < x; i++)
{
if((x%i) == 0)
{
printf("%d\n", i);
sum = sum+i;
}
}
return sum;
}
int main() {
int x, i;
printf("\nInput an integer: ");
scanf("%d", &x);
printf("All the divisor of %d are: ", x);
printf("the sum of divisor is %d ", sum_of_divisor(x));
return 0;
}
Output:
Input an integer: 10
All the divisor of 10 are: 1
2
5
the sum of divisor is 8
After checking if i is a divisor of x, you should then store that value in another variable, for example m.
Repeat until a new divisor i is higher than that number. Add this new value to m.
program wherein there shouldn't be negatives in the equation and it will end when 0 as input
#include <stdio.h>
int main() {
double number, sum = 0;
int average;
int count;
do {
printf("Enter a number: ");
scanf("%lf", &number);
count++;
if (number > 0)
sum += number;
average = sum / (count);
} while (number != 0);
printf("Average is %.1lf", average);
return 0;
}
In your program you are counting all numbers independent on whether they are positive or negative
do {
printf("Enter a number: ");
scanf("%lf", &number);
count++;
//...
Also it does not make a sense to calculate the average within the do while loop.
average = sum / (count - 1);
And it is unclear why the variable average has the type float instead of double.
float average;
And you forgot to initialize the variable count.
Pay attention to that the user can enter neither positive number.
And as it follows from the title of your question you are going to enter integer numbers not double.
The program can look the following way
#include <stdio.h>
int main( void )
{
double sum = 0.0;
double average = 0.0;
size_t count = 0;
while( 1 )
{
printf( "Enter a number: " );
int number;
if ( scanf( "%d", &number ) != 1 || number == 0 ) break;
if ( number > 0 )
{
sum += number;
++count;
}
}
if ( count != 0 ) average = sum / count;
printf( "Average is %f\n", average );
return 0;
}
Firstly, you didn't initialize your count variable. That said, you should increment the count variable only when a non negative value is encountered. Unrelated but I'd recommend you calculate the average outside the loop, as below:
#include <stdio.h>
int main()
{
double number, sum = 0;
float average;
int count = 0;
do
{
printf("Enter a number: ");
scanf("%lf", &number);
if (number > 0)
{
sum += number;
count++;
}
} while (number != 0); //should it stop with 0 or 1? assuming 0
average = sum / count;
printf("Average is %.1lf\n", average);
return 0;
}
First of all, always enable your compiler's warnings. I use -Wall -Wextra -pedantic with gcc and clang. This would have caught the first of the problems listed below if nothing else.
There are many problems.
count is not initialized.
Negative numbers aren't included in the sum as you claim, but they do affect the average because you increment count for negative numbers too.
The assignment asks for you to loop until you get zero, but you loop until you get -1.
The formula for average isn't sum / (count - 1).
average is calculated over and over again for no reason.
You don't handle the case where no inputs are entered, leading to a division by zero.
average is a float, which is odd since you it's built from double values.
You should check the value returned by scanf for errors or end of file.
You don't emit a line feed after your output.
#include <stdio.h>
int main(void) {
int count = 0.0;
double sum = 0.0;
while (1) {
printf("Enter a number: ");
double number;
if ( scanf("%lf", &number) < 1 ) // Invalid input or EOF.
break;
if ( number == 0.0 )
break;
if ( number < 0.0 )
continue;
count++;
sum += number;
}
if (count) {
double average = sum / count;
printf("Average is %.1lf\n", average);
} else {
printf("Nothng to average\n");
}
return 0;
}
i need to assign these values to the array but i keep receiving assignment to expression error, I'm wondering how I can assign values to those arrays. i try assigning the inputted value of arr into num but i believe I am missing something in that expression.
this codes job is to read the input of a temperature assign the input into a category of the type of day and give the average of the temperature
Enter a high temp reading (-99 to quit)> 100
Enter a high temp reading (-99 to quit)> 0
Enter a high temp reading (-99 to quit)> 50
Enter a high temp reading (-99 to quit)> -99
Hot days: 1
Pleasant days: 0
Cold days: 2
The average temperature was 50.00 degrees.
this is what an output should look like
#include <stdio.h>
int main(void){
//declare variables
int num[30];
int Cday[30];
int Pday[30];
int Hday[30];
int total;
int total_ave;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr;
//ask input store into num, then decides if the number goes into which array
do{
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
num = arr;
if(arr == -99)
{
break;
}
else if(arr<=60 && arr>=0){
Cday = num;
}
else if(arr>=60 && arr<=84){
Pday = num;
}
else if(arr<=84 && arr>=200){
Hday = num;
}
}while(num >=0);
//calculating the average
total = sizeof(num);
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
//to print the amount of times each day was in a category
NHday = sizeof(Hday)/sizeof(Hday[0]);
NPday = sizeof(Pday)/sizeof(Pday[0]);
NCday = sizeof(Cday)/sizeof(Cday[0]);
//printing the final statement once all the values are calculated
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
printf("The average temperature was %.2f degrees.", ave);
//stops compiling when -99 is entered not collected as information
//
//
return(0);
}
i would also appreciate to know if how i am calculating how many items are in the array with the NHday CDay and Pday Calculations is the correct way to do it.
i am also wondering if my calculation for the average is correct. any help is appreciated thank you.
You don't need arrays for the different types of days, just counter variables, which you increment based on the temperature range.
total should not be sizeof(num) for two reasons:
sizeof counts bytes, not array elements.
You only want the count of inputs, not the total size of the array. You can get the total by adding the counters of each type of day.
You don't have to put the temperatures in an array. Just add each day's temperature to the total_ave variable after the user enters it.
When you're calculating ave, you need to cast one of the variables to double so that floating point division will be used instead of integer division.
#include <stdio.h>
int main(void){
//declare variables
int num;
int total;
int total_ave = 0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
//ask input store into num, then decides if the number goes into which array
while (1) {
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &num);
if(num == -99)
{
break;
}
else if(num<=60){
NCday++;
}
else if(num<=84){
NPday++;
}
else {
NHday++;
}
total_ave += num;
}
//calculating the average
total = NHday + NPday + NCday;
ave = (double)total_ave / total;
//printing the final statement once all the values are calculated
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
printf("The average temperature was %.2f degrees.", ave);
return(0);
}
You are assigning arr value to num array without using index. Your while loop should be like
while(i<30){
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
if(arr==-99){
break;
}
num[i] = arr;
i++;
total++;
if(arr<=60 && arr>=0){
NCday++;
}
else if(arr>=60 && arr<=84){
NPday++;
}
else if(arr>=84 && arr<=200){
NHday++;
}
}
Now
total = sizeof(num);
This will give you the total size of num array which is 30*4=120 as int data type has 4 byte size.
If you are not filling all 30 values then you need to know the number of values your are going to use from that array. That's why I am incrementing total as I am inserting values in num array which will help later.
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
If you just want to know the count of cold, hot days then you can increment them in while loop and print those.
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
And first initialize the variables.
int num[30];
int total=0;
int total_ave=0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr=0;
int i=0;
The for loop I wrote to calculate sum of numbers is not working properly.
The program finds the average of the given numbers using a dynamically allocated array.
#include<stdio.h>
main()
{
int no_of_nos;
printf("Enter the number of numbers yuo want to do average : ");
scanf("%d",&no_of_nos);
int x[no_of_nos+1];
int i;
for(i=0;i<no_of_nos;i+=1)
{
printf("Enter the values for the element of the array : ");
scanf("%d",&x[i]);
}
int sum=0,j;
for(j=0;j<no_of_nos;j++)
{
sum = x[j] + x[j+1];
}
printf("The sum of the given numbers = %d \n",sum);
float average = sum / no_of_nos;
printf("The average of the given numbers = %0.2f",average);
}
Your second loop is wrong, it should read:
for( i=0; i<no_of_nos; i++ ) {
sum += x[i];
}
No need for another integer, 'i' will do fine. You could optimise it further by moving the sum up before the first loop then totalise sum as you enter the values. Do you need the array?
int sum = 0;
for( int i=0; i<no_of_nos; i++ ) {
int x;
printf("Enter the values for the element of the array : ");
scanf("%d", x);
sum += x;
}
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
There is no sense to declare the array x with no_of_nos+1 elements instead of no_of_nos elements. So the declaration of the array should look like
int x[no_of_nos];
In fact an array is not required if you need only to calculate the average of entered numbers.
It is better initially to declare the variable sum as having type float or double. Otherwise this expression statement
float average = sum / no_of_nos;
does not make a great sense because the expression sum / no_of_nos evaluates as an integer expression that is in any case the result of the expression is an integer.
However if the variable sum declared like for example float then the expression sum / no_of_nos will have the type float.
For example
float sum = 0;
//...
float average = sum / no_of_nos;
Compare two outputs of the average in the following demonstrative program.
#include <stdio.h>
#define N 2
int main(void)
{
int a[N] = { 1, 2 };
int sum1 = 0;
for ( size_t i = 0; i < N; i++ ) sum1 += a[i];
float average = sum1 / N;
printf( "The first average is equal to %0.2f\n", average );
float sum2 = 0;
for ( size_t i = 0; i < N; i++ ) sum2 += a[i];
average = sum2 / N;
printf( "The second average is equal to %0.2f\n", average );
return 0;
}
The program ooutput is
The first average is equal to 1.00
The second average is equal to 1.50
This loop
for(j=0;j<no_of_nos;j++)
{
sum = x[j] + x[j+1];
}
also does not make sense. It is enough to write
for(j=0;j<no_of_nos;j++)
{
sum += x[j];
}
The program to calculate the Average of given numbers can be written without the use of arrays.
The following program finds the average of given numbers and gives the answer upto two decimal places:
#include<stdio.h>
main()
{
int n,i=0,j;
float sum=0;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(;i<n;i++)
{
printf("Enter the number : ");
scanf("%d",&j);
sum = sum + j;
}
printf("The sum of the given numbers = %0.2f \n",sum);
float average = sum / n;
printf("The Average of the given numbers = %0.2f",average);
}
#include <stdio.h>
#include <conio.h>
int getn(int n, int i);
int main()
{
int n, i;
getn(n, i);
getch();
return 0;
}
int getn(int n, int i)
{
int even = 0;
int odd = 1;
int avg;
printf("Enter ten integers: \n");
for (i = 1 ; i <= 10 ; i++)
{
printf("Integer %d: ", i);
scanf("%d", &n);
if ( n % 2 == 0 )
{
even = even + n;
}
else
{
odd = odd * n;
}
}
avg = even / 10;
printf("\n\nAverage of even numbers: %d", avg);
printf("\nProduct of odd numbers: %d", odd);
}
It seems the even calculations worked but when it comes to odd it gives the wrong answer. Please help
Our instructor wants us to use looping or iterations. No arrays. Please help me
First, your C code needs some correction:
at least give the prototype of getn before using it
getn is defined to return an int and doesn't return anything. Either replace int with void or return a value.
Second,
Your code computes the product of ten numbers, if this product is too big, it cannot be store as-is in an int. For example, it works well if you enter ten times number 3, the result is 59049, but if you enter ten times number 23, it will answer 1551643729 which is wrong because 23^10=41426511213649 but that can't be stored in an int. This is known as arithmetic overflow.
Your average is bad, because you sum ints, but the average is (in general) a rational number (average(2,3)=2.5 isn't it ?). So double avg = out/10.0; (means compute a floating division) and printf("Average %f\n",avg); would be better.