I'm trying to create a calculator, but for my mix and max, I'm not getting the correct results. I'm pretty sure this is bad but how to fix it? -
for (i = 1; i <= n; i = i + 1) {
-
printf("Enter data item %d: ", i);
-
scanf("%f", &data);
-
-
sumx = sumx + data;
-
sumx2 = sumx2 + (data*data);
-
-
}
-
-
-
mean = sumx / n;
-
var = 1.0/(n-1) * (sumx2 - (1.0/n)*sumx*sumx);
-
sd = sqrt(var);
-
-
min = 0;
-
max = 0;
-
-
if (data > max){
-
max = data;
-
}
-
else if(data < min)
-
min = data;
-
/* print the computed values */
-
-
-
-
printf("\n");
-
printf(" mean = %f\n", mean);
-
printf(" variance = %f\n", var);
-
printf("std. dev. = %f\n\n", sd);
-
printf("The minimum number in the list is %d.\n", min);
-
printf("THe maximum number in the list is %d.\n", max);
-
-
-
return 0;
-
}
-
Oops: - printf("How many numbers do you need to enter (n)? ");
-
scanf("%d", &n);
-
-
while(n < 0) {
-
printf("You must enter a positive number. \n");
-
scanf("%d", &n);
- }
-
The two scanf calls put data into the same variable.
16 1470
I tend to skip the else -
if(data >= max)
-
max = data;
-
if(data <= min)
-
min = data;
-
Tried that and doesn't work. I updated the post with more code
what data types are you using on your variables posted?
your parens may be important. 3 / 4 * 4 / 10 may yield goofy results if you really mean (3 / 4) * (4 / 10)
look out for any integer division
Sorry not exactly sure what you mean. Everything in the code works except for the 'min' and 'max' thing. It's just printing out the last number the user enters.
min and max are both set as integers if that's what you're asking.
It's just printing out the last number the user enters
I didn't catch that in the code you posted - sorry. You'll want to check data for max or min within the loop. -
for(i=0; i < n; ++i)
-
{
-
// prompt the user for input
-
// store user input in data
-
// check data for either max and min
-
// manipulate sumx and sumx2
-
}
-
-
// other stuff
-
Here's my for loop, but still the same problem. I deleted what I had before -
for (i = 1; i <= n; i++) {
-
printf("Enter data item %d: ", i);
-
scanf("%f", &data);
-
if (data > max){
-
max = data;
-
}
-
if(data < min) {
-
min = data;
-
}
-
sumx += data;
-
sumx2 += (data*data);
-
-
}
What are the data types of min, max, data, sumx, var, sd, etc...
They better all be floating point or all be integers. You cannot mix integer and floating point arithmetic.
I originally had min and max as integers, but changed them to floats and altered the code. Still same results however
It appears that min and max are set outsde the loop so you are using the last value of data. Maybe the min/max should be placed inside the loop where it will be exposed to each data value entered.
Please use integers. Floating point comparisons are harder to work with.
I did change my code to put it in the for loop, but still same problem
So what does your code look like now?
#include <stdio.h> -
#include <math.h>
-
-
int main() {
-
int i; /* loop variable */
-
int n; /* number of data items */
-
float mymin; /*Minumum value */
-
float mymax; /*Maximum value */
-
float data; /* data item */
-
float sumx; /* sum of data items */
-
float sumx2; /* sum of squared data items */
-
float mean; /* mean of the data */
-
float var; /* variance of the data */
-
float sd; /* std. deviation of data */
-
-
/* Get the number of data items from the user */
-
-
printf("How many numbers do you need to enter (n)? ");
-
scanf("%d", &n);
-
-
while(n < 0) {
-
printf("You must enter a positive number. \n");
-
scanf("%d", &n);
-
}
-
-
sumx = sumx2 = 0.0;
-
-
-
-
for (i = 1; i <= n; i = i + 1) {
-
printf("Enter data item %d: ", i);
-
scanf("%f", &data);
-
if(data > mymax){
-
mymax = data;
-
}
-
if(data < mymin){
-
mymin = data;
-
}
-
sumx = sumx + data;
-
sumx2 = sumx2 + (data*data);
-
-
}
-
-
-
-
mean = sumx / n;
-
var = 1.0/(n-1) * (sumx2 - (1.0/n)*sumx*sumx);
-
sd = sqrt(var);
-
-
-
-
printf("\n");
-
printf("mean = %f\n", mean);
-
printf("variance = %f\n", var);
-
printf("std. dev. = %f\n\n", sd);
-
printf("Minimum value is %f\n", mymin);
-
printf("Maximum value is %f/n", mymax);
-
-
return 0;
-
}
-
Sorry about indentation problems.
Where are mymax and mymin defined? What are their initial values?
Sorry about that, I kinda copied and pasted some and freehanded the rest. Float min = mymin and max = mymax
Oops: - printf("How many numbers do you need to enter (n)? ");
-
scanf("%d", &n);
-
-
while(n < 0) {
-
printf("You must enter a positive number. \n");
-
scanf("%d", &n);
- }
-
The two scanf calls put data into the same variable.
Ohhh haha I see that now. THanks, I fixed it. Works great now
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mwh |
last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a
calculator with javascript. I can't get this to work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
|
by: Rafael |
last post by:
Hi Everyone,
I need some help with my calculator program. I need my program to
do 2 arguments and a 3rd, but the 3rd with different operators. Any
help would be great.
Here is my code....
...
|
by: Ken Brady |
last post by:
I'm on a team building some class libraries to be used by many other
projects.
Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be...
|
by: Z D |
last post by:
Hello,
I'm having a strange problem that is probably due to my lack of
understanding of how threading & COM Interop works in a WinForms.NET
application.
Here's the situation:
I have a 3rd...
|
by: Art |
last post by:
Hi,
In part of my application the user may need to do a simple arithmetic
calculation in order to get the value to put in a text box. I was thinking
that it would be good if I could display the...
|
by: Head In A Pan |
last post by:
I'm trying to adapt an old piece of javascript to allow it to work with IE7 (which it wont... yet)
At the start of my js there is a line of code:
var ns =...
|
by: adweaver |
last post by:
I've been pulling my hair out on this one for a while. I even have a working version of the code and a side by side comparison has led me nowhere.
If anyone can provide some insight on what I've...
|
by: danielmessick |
last post by:
Hello, I'm trying to fix my Java applet to have a fully functional calculator. I managed to create the buttons and have the +, -, /, *, =, and clear buttons WORKING. But, I cant seem to figure out...
|
by: Curtis Rutland |
last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |