C program calculate average - c

the average is not correct for this code. How to solve the average with this code. I try all of things but every time its same. It shows last number average only.
#include<stdio.h>
void compute(int anumber,int *max,int *min,float *average,int count);
int main ()
{
int max=0,min=100;
float average=0;
int number,i=0;
printf("\nenter a number:");
scanf("%d",&number);
while(number>0){
i++;
compute(number,&max,&min,&average,i);
printf("\nenter a number:");
scanf("%d",&number);
}
printf("\naverage %.2f",average);
printf("\ncount %d",i);
printf("\nmin %d",min);
printf("\nmax %d",max);
return 0;
}
void compute(int anumber,int *max,int *min,float *average,int count)
{
float total;
total=total+anumber;
*average=total/count;
++count;
if(anumber>*max)
*max=anumber;
else if(anumber<*min)
*min=anumber;
}

Issue #1: Total and number are uninitialized.
In C no initialization of variables is done for you. What is the value of total after:
float total;
The answer: Whatever was in memory at the location it was allocated. It could 0, 20, 94, 15, 7, or -2.3 billion, unless you give it a value it has an arbitrary one. It's value is indeterminate.
total=total+anumber;
By adding anumber to an unknown number you have no idea what total will become.
Issue #2: You don't calculate total.
The total is calculated by taking all the numbers in a set and summing them.
I.E. With the numbers 2,4,6 the total is 2 + 4 + 6 which equals 12. The first thing you should note is the presence of repetition. The addition is a common aspect that can be abstracted. We normally abstract repetition into a loop i.e.
int i;
float total = 0;
for(i = 0; i <= 6; i+=2)
{
total += i;
}
You need a similar loop in your program.
Your compute function is void, it cannot return a value. There are a handful of ways to fix total so that you may calculate the average.
Define total in the global scope for example, this is done by moving it outside of any function body. You'll have to remove the definition of total from within your function, as the variable in the closest outer scope will be used.
Make total static, it will not be automatically disposed of between each function call. This allows you to pass in values and allow the total to remain.
Regardless of your choice you'll have to make some changes, as of now you are not actually calculating the average. Further details on the assignment or task you were given would benefit everyone. C has few keywords but many ways of using them, in order to fully answer your question we must know:
What is the program supposed to do? Anticipated input/output helps a lot. In order to come up with a solution you must first fully understand the problem. It can be a challenge to extract intended functionality even from well written and working code, it can be near impossible to do so with broken code.
Were you given specifications? Do you understand them? I see from your comment that you are not allowed to change function headers/prototypes, what other restrictions do you have?
One final note. I'm assuming this is a homework assignment, as someone who has taken many classes based around C programming and who has taught some myself I can guarantee you - You will get the most out of the exercise by receiving only the help that is necessary, strive to think about the problem as much as you can on your own as well.

Your variable "total" is local to the compute() function, so it does not contain the total of all the input numbers - it is as new variable each time compute() is called, which is every time through the loop.
If you want to process each number one at a time, you can find the total within the compute function, if you make total a global variable. Then the correct approach would be find the average once, by dividing that total by the number of values.

Related

Is there a way to write a recursive function that multiplies the even numbers from 1 to n using c?

I've found similar questions that do that to the sums of even numbers, but when i try to change that to the product of these, it always ends up printing out 0.
#include<stdio.h>
int SumEven(int num1, int num2)
{
if(num1>num2)
return 0;
return num1*SumEven(num1+2,num2);
}
int main()
{
int num1=2,num2;
printf("Enter your Limit:");
scanf("%d",&num2);
printf("Sum of all even numbers in the given range is: %d",SumEven(num1,num2));
}
this is an example of one of those i tried to adapt but it only returns 0, any ideas?
You need to post code for a question like this. But the probable cause- when you do a sum, you start with an initial value of 0. When you do products, it needs to be 1. Otherwise you multiply 0*i, which is always 0.
After a bit of thought, I am revising my original answer even though it is a workable solution. Actually, all that you would need to do is change one line of code. Instead of the following linesof code.
if(num1>num2)
return 0;
You just need to do the following.
if(num1>num2)
return 1;
Following were some small limit test results that agreed with manual calculations.
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:4
Sum of all even numbers in the given range is: 8
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:8
Sum of all even numbers in the given range is: 384
:~/C_Programs/Console/MultEven/bin/Release$ ./MultEven
Enter your Limit:10
Sum of all even numbers in the given range is: 3840
Try that out.
Regards.

Assigning changing numbers to a single variable

Just screwing around in C... wanted to make a program that calculated the average of whatever numbers the user input.
The program works fine, I just can't 'logic' out how to do the next part.
I need to be able to take each number they input and average them, using the number they input for the first scanf to divide by. I only assigned one variable for each number they input, but after about a second of looking at the code I realized I would need to use calculus or some programming trick to be able to do this (effectively) infinitely. Basically, the variable needs to change each time so the sum can be taken then divided over the total number of numbers. I'm ranting...
Can anyone who can understand my stupid problem give me some pointers? That'd be great...
My includes and the int main () are there, don't worry. Just felt no need to clutter it with already known stuff. Also, I don't do shorthand anything- I feel no need to as of now.
// Base variables
int iUserReq, iNumCounter = 0;
// Each individual number
double dUserNum = 0.0;
// Calculation
double dNumSum = 0.0, dNumAvg = 0.0;
// Ask user for the number of variables to be averaged... will come in handy
printf("Please input how many numbers you would like to average together, as a number. For example, 10.\nTry to keep it low, because you're going to be putting them all in manually. > ");
scanf("%d", &iUserReq);
// If user inputs 0 or negative number, keep asking until they put in a positive number
while(iUserReq <= 0)
{
printf("Please input a number greater than 0. > ");
scanf("%d", &iUserReq);
}
// This adds a counter, so for the number of numbers the user wants to average, it will loop that many times and ask for an input that many times
// I.e. they want to average 10 numbers, it asks for 10 numbers
// THIS IS WHERE I'M STUCK... HELP?
while(iNumCounter < iUserReq)
{
printf("Please input number. > ");
scanf("%lf", &dUserNum);
iNumCounter = iNumCounter + 1;
}
return 0;
Thanks...
Bagger
OK, I'm going to bite and give you the solution.
At the top of your program:
double sum = 0.0;
After you scanf() for each number:
sum += dUserNum;
Just before return 0:
printf("%f\n", sum / iUserReq);
Do you understand why this works?

Abort trap: 6 (Calculating a long number factorial)

I am following the following function to calculate factorials of big numbers link, and I would like to understand a bit more why some things are happening...
#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];
int main(){
int num;
int i;
printf("Enter any integer number : ");
scanf("%d",&num);
fact[0]=1;
factorialof(num);
printf("Factorial is : ");
for(i=length;i>=0;i--){
printf("%d",fact[i]);
}
return 0;
}
void factorialof(int num){
int i;
for(i=2;i<=num;i++){
multiply(i);
}
}
void multiply(int num){
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++){
arr[i]=fact[i];
}
for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;
//printf("%d ",r);
}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}
My questions are:
What is the real meaning of the MAX constant? What does it mean if it's bigger or smaller?
I have found out that if I have a MAX = 10000 (as in the example), I can calculate up to 3250! If I try with 3251! I get a 'Abort trap: 6' message. Why is that number? Where does it come from?
Which would be the difference if I compile this code for a 32-bit machine with the flag -m32? Would it run he same as in 64-bit?
Thanks!
As Scott Hunter points out, MAX is the maximum number of elements in the fact and arr arrays, which means it's the maximum number of digits that can occur in the result before the program runs out of space.
Note that the code only uses MAX in its array declarations. Nowhere does it use MAX to determine whether or not it's trying to read from or write to memory beyond the end of those arrays. This is a Bad Thing™. Your "Abort trap: 6" error is almost certainly occurring because trying to compute 3251! is doing exactly that: using a too-large index with arr and fact.
To see the number of digits required for a given factorial, you can increase MAX (say, to 20,000) and replace the existing printf calls in main with something like this:
printf("Factorial requires %d digits.\n", length + 1);
Note that I use length + 1 because length isn't the number of digits by itself: rather, it's the index of the array position in fact that contains the most-significant digit of the result. If I try to compute 3251!, the output is:
Factorial requires 10008 digits.
This is eight digits more than you have available in fact with the default MAX value of 10,000. Once the program logic goes beyond the allocated space in the array, its behavior is undefined. You happen to be seeing the error "Abort trap: 6."
Interestingly, here's the output when I try to compute 3250!:
Factorial requires 10005 digits.
That's still too many for the program to behave reliably when MAX is set to 10,000, so the fact that your program calculates 3250! successfully might be surprising, but that's the nature of undefined behavior: maybe your program will produce the correct result, maybe it will crash, maybe it will become self-aware and launch its missiles against the targets in Russia (because it knows that the Russian counterattack will eliminate its enemies over here). Coding like this is not a good idea. If your program requires more space than it has available in order to complete the calculation, it should stop and display an appropriate error message rather than trying to continue what it's doing.
MAX is the number of elements in fact and arr; trying to access an element with an index >= MAX would be bad.
Error messages are often specific to the environment you are using, which you have provided no details for.
They are not the same, but the differences (for example, the size of pointers) should not affect this code in any discernible way.

Print the number that appears the maximum number of times in the sequence

Recently I have gone through a problem like this
Write a program to read a sequence of N integers and print the number that
appears the maximum number of times in the sequence.
CONSTRAINTS
1 <= N <= 10000
The integers will be in the range [-100,100]
I have writen the following code:
main()
{int arr[201],max=0,maxelement,n,i,num;
int t;
scanf("%d",&n);
int *storenum=(int *)malloc(sizeof(int)*n);
for(i=0;i<201;i++)
{
arr[i]=0;
}
for(i=0;i<n;i++)
{
scanf("%d",&num);
storenum[i]=num;
if(num<=100 && num>=-100)
{
arr[num+100]=arr[num+100]+1;
}
}
for(i=0;i<n;i++)
{
int t=storenum[i]+100;
if(arr[t]>max)
{ maxelement=storenum[i];
max=arr[t];}
}
printf("\n\n%d",maxelement);
getch();
}
Now I think this code is not optimized one...I want to have a solution that would have less time and space complexity and better solution.
You don't have to iterate through all N items a second time, just iterate through the 201 counts looking for the largest.
Your code looks very close to optimal. It can be made only a little better given your constraints.
Use memset when you mean memset. Yes the compiler will pick it up 99% of the time, but why bother?
for(i=0;i<201;i++) arr[i]=0; // becomes
memset(arr, '\0', sizeof(arr))
The count for any given item can be no larger than 10000. That means it can be held in a ushort!
int arr[201]; // becomes
uint16_t arr[201];
Your second loop should go from for(i = -100; i <= 100; i++) instead, and just loop over arr.
Your code is butt-ugly. Use a consistent indentation, brace style, and don't cram 50 declarations on the same line. Spaces are okay between elements. arr should probably be named something meaningful, I like counts. It kind of looks like you're trying to use gnu style C, but K&R works too, really just pick any style guide and glance through it.

Memory leaks in C?

I'm currently learning to program in C. In one of the tasks in my assignment, I have to make a histogram (drawn by basic console output, like this: http://img703.imageshack.us/img703/448/histogram.jpg) to measure the number of characters in a text file (standard for this assignment is 1.3 MB). I did make a function like this:
int *yAxisAverageMethod(int average, int max, int min)
{
int *yAxis;
int i=0;
for (i=0;i<20;i++)
{
*(yAxis+i)=0;
}
/*
int length=sizeof(data)/sizeof(int);
*/
int lower_half_interval=average/10;
int upper_half_interval=(max-average)/10;
int current_y_value=min;
for (i=0;i<11;i++)
{
if (i==10){
*(yAxis+10)=average;
break;
}
*(yAxis+i)=current_y_value;
current_y_value+=lower_half_interval;
}
current_y_value+=average+upper_half_interval;
printf("Current y value:%d\n",current_y_value);
printf("Current max value:%d\n",max);
for (i=11;i<20;i++)
{
*(yAxis+i)=current_y_value;
current_y_value+=upper_half_interval;
}
return yAxis;
}
In this function, I intend to return an array of 20 integers, in order to make a y axis. I find the average of all characters, then used 20 lines of the console to display it. The lower 10 lines are used to display the lower than average values of the total amount of characters, and 10 lines are used to display the upper part. Each step in the y axis of the lower half is calculated by (average - min)/10, and each step in the y axis of the upper part is calculated by (max - average)/10. This is my method to draw the histogram, because I want to display the variants between values.
In the main method, I have this function call:
int *yAxis;
yAxis=yAxisAverageMethod(average,max,min);
I got a segmentation fault when I ran the function. In netbean GCC++ compiler, it works fine. Howerver, when I ran it on the university machines (which I have to compile it on command line and edit in Vi), I got the error. I guess it is because Netbean has its own memory manager? I don't understand.
Edited: I will ask about merge sort in anothe question.
*yAxis is a wild pointer. You never allocate memory for the int array you want to use.
int *yAxis = malloc(sizeof(int) * 20);
You are returning a pointer to nothing.
Where inside the function do you tell the computer to reserve some memory for *yAxis?
yAxis is a point, and you did not initialize it. it will point to unknown space what depends on the compiler. you should apply some memory for it firstly.
yAxis = malloc(sizeof(int)*20);
Don't forget to free() it in the caller.

Resources