473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++ Calculator App, can't get MIN & MAX to work.

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?
Expand|Select|Wrap|Line Numbers
  1. for (i = 1; i <= n; i = i + 1) {
  2.     printf("Enter data item %d: ", i);
  3.     scanf("%f", &data);
  4.  
  5.     sumx = sumx + data;
  6.     sumx2 = sumx2 + (data*data);
  7.  
  8. }
  9.  
  10.  
  11. mean = sumx / n;
  12. var = 1.0/(n-1) * (sumx2 - (1.0/n)*sumx*sumx);
  13. sd = sqrt(var);
  14.  
  15. min = 0;
  16. max = 0;
  17.  
  18.     if (data > max){
  19.       max = data;
  20.     }
  21.     else if(data < min)
  22.       min = data;
  23.     /* print the computed values */
  24.  
  25.  
  26.  
  27.     printf("\n");
  28.     printf("     mean = %f\n", mean);
  29.     printf(" variance = %f\n", var);
  30.     printf("std. dev. = %f\n\n", sd);
  31.     printf("The minimum number in the list is %d.\n", min);
  32.     printf("THe maximum number in the list is %d.\n", max);
  33.  
  34.  
  35.     return 0;
  36. }
  37.  
Apr 1 '14 #1

✓ answered by weaknessforcats

Oops:

Expand|Select|Wrap|Line Numbers
  1. printf("How many numbers do you need to enter (n)? ");
  2.     scanf("%d", &n);
  3.  
  4.     while(n < 0) {
  5.       printf("You must enter a positive number. \n");
  6.       scanf("%d", &n);
  7.     }
  8.  
The two scanf calls put data into the same variable.

16 1470
divideby0
131 128KB
I tend to skip the else

Expand|Select|Wrap|Line Numbers
  1. if(data >= max)
  2.    max = data;
  3. if(data <= min)
  4.    min = data;
  5.  
Apr 1 '14 #2
Tried that and doesn't work. I updated the post with more code
Apr 1 '14 #3
divideby0
131 128KB
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
Apr 2 '14 #4
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.
Apr 2 '14 #5
divideby0
131 128KB
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.

Expand|Select|Wrap|Line Numbers
  1. for(i=0; i < n; ++i)
  2. {
  3.    // prompt the user for input
  4.    // store user input in data
  5.    // check data for either max and min
  6.    // manipulate sumx and sumx2
  7. }
  8.  
  9. // other stuff
  10.  
Apr 2 '14 #6
Here's my for loop, but still the same problem. I deleted what I had before


Expand|Select|Wrap|Line Numbers
  1.  for (i = 1; i <= n; i++) {
  2.     printf("Enter data item %d: ", i);
  3.     scanf("%f", &data);
  4.     if (data > max){
  5.       max = data;
  6.     }
  7.     if(data < min) {
  8.       min = data;
  9.     }
  10.     sumx += data;
  11.     sumx2 += (data*data);
  12.  
  13.   }
Apr 2 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 2 '14 #8
I originally had min and max as integers, but changed them to floats and altered the code. Still same results however
Apr 2 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 2 '14 #10
I did change my code to put it in the for loop, but still same problem
Apr 2 '14 #11
weaknessforcats
9,208 Expert Mod 8TB
So what does your code look like now?
Apr 2 '14 #12
#include <stdio.h>
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2.  
  3. int main() {
  4.     int i;              /* loop variable             */
  5.     int n;              /* number of data items      */
  6.     float mymin;            /*Minumum value              */
  7.     float mymax;            /*Maximum value              */
  8.     float data;         /* data item                 */
  9.     float sumx;         /* sum of data items         */
  10.     float sumx2;        /* sum of squared data items */
  11.     float mean;         /* mean of the data          */
  12.     float var;          /* variance of the data      */
  13.     float sd;           /* std. deviation of data    */
  14.  
  15.     /* Get the number of data items from the user */
  16.  
  17.     printf("How many numbers do you need to enter (n)? ");
  18.     scanf("%d", &n);
  19.  
  20.     while(n < 0) {
  21.       printf("You must enter a positive number. \n");
  22.       scanf("%d", &n);
  23.     }
  24.  
  25.  sumx = sumx2 = 0.0;
  26.  
  27.  
  28.  
  29.     for (i = 1; i <= n; i = i + 1) {
  30.       printf("Enter data item %d: ", i);
  31.       scanf("%f", &data);
  32.       if(data > mymax){
  33.         mymax = data;
  34.       }
  35.       if(data < mymin){
  36.         mymin = data;
  37.       }
  38.       sumx = sumx + data;
  39.       sumx2 = sumx2 + (data*data);
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.     mean = sumx / n;
  46.     var = 1.0/(n-1) * (sumx2 - (1.0/n)*sumx*sumx);
  47.     sd = sqrt(var);
  48.  
  49.  
  50.  
  51.     printf("\n");
  52.     printf("mean = %f\n", mean);
  53.     printf("variance = %f\n", var);
  54.     printf("std. dev. = %f\n\n", sd);
  55.     printf("Minimum value is %f\n", mymin);
  56.     printf("Maximum value is %f/n", mymax);
  57.  
  58.     return 0;
  59. }
  60.  
Sorry about indentation problems.
Apr 2 '14 #13
donbock
2,426 Expert 2GB
Where are mymax and mymin defined? What are their initial values?
Apr 2 '14 #14
Sorry about that, I kinda copied and pasted some and freehanded the rest. Float min = mymin and max = mymax
Apr 2 '14 #15
weaknessforcats
9,208 Expert Mod 8TB
Oops:

Expand|Select|Wrap|Line Numbers
  1. printf("How many numbers do you need to enter (n)? ");
  2.     scanf("%d", &n);
  3.  
  4.     while(n < 0) {
  5.       printf("You must enter a positive number. \n");
  6.       scanf("%d", &n);
  7.     }
  8.  
The two scanf calls put data into the same variable.
Apr 3 '14 #16
Ohhh haha I see that now. THanks, I fixed it. Works great now
Apr 3 '14 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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"...
6
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.... ...
175
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...
8
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...
3
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...
1
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 =...
9
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...
1
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...
0
Curtis Rutland
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....
0
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$) { } ...
0
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.