C program Beginner lvl

Find the Max, Min, Sum, Average, Variance and Standard Deviation

Page 1 of 1

9 Replies - 1319 Views - Last Post: 25 May 2008 - 12:41 AM Rate Topic: -----

#1 lordace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-May 08

C program Beginner lvl

Posted 23 May 2008 - 09:35 AM

This is what I have so far, but I think I am far from done. I have to add the Variance and the standard deviation (which the formular is on the attached file) also i need to ask the user if he or she wants to repeat the process. Please I need help I have to turn this in tommorrow I been working on this for more than 10 straight and cannot get.
#include <stdio.h>
#include <stdlib.h>

void main(void)
{
	 int sum = 0;
	 int number = 0;
	 int value = 0;
	 int min = value;
	 int max = value;
	 int average = 0;

	 printf ("Please enter a series of numbers terminated by -1");
	 scanf ("%d", &number);

	 average == number;

	 while (number > 0)
	 {
	   number = number - 1;
	   if (value > max)
		   max = value;

	   else if (value < min)
		   min = value;

	 printf ("Next number");
	 scanf ("%d, &value");
	 }

	 if (average == 0)

	 printf ("No Entry");

	 else
	 {
	   average = sum / average;
	   printf ("Maximum value entered: %max\n");
	   printf ("Minimum value entered: %min\n");
	   printf ("Mean: %average");
	 }

	  printf("\n\n");
	  system("PAUSE");
	  return 0;
}

Attached File(s)


This post has been edited by jayman9: 23 May 2008 - 11:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C program Beginner lvl

#2 lordace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-May 08

Re: C program Beginner lvl

Posted 23 May 2008 - 11:32 AM

I tried to re-write the program another way please at least tell me if i am on the right track or if i am missing something. THANK YOU.
#include <stdio.h>
#include <stdlib.h>

void main(void)
{
	 double x;
	 double max;
	 double min;
	 double sum;
	 double mean;
	 double var;
	 double std_dev;
	 double total = 0;

	 printf("Please enter a series of numbers terminated by -1: ");
	 scanf("%lg", &x);

	 total = total + x;

	 while(x > 0)
	 {
	   if (x > max)
		 max = x;

	   else if (x < min)
		 min = x;

	 printf("Enter another number: ");
	 scanf("%lg", &x);
	 }

	 while (x = -1)
	 {
	   printf("You entered %g values", x);
	   printf("Maximum value entered was: %g. \n", max);
	   printf("Minimum number entered was: %g. \n", min);
	 }

	 printf("The program ran sucsesful");


	 printf("\n\n");
	 system("PAUSE");
	 return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Whizzy*


Reputation:

Re: C program Beginner lvl

Posted 23 May 2008 - 07:24 PM

Well, first off...
You assigned the variable
double min, then in your loop, you say:
if x<min then min=x... That can't work. For the simple reason, any answer less than 0 will execute. min is already set to 0, you can't go lower...

I changed a few prinf's to cout's just as a personal preference, you can change that back if you wish, no problem. Also, you had have x reading your number of values entered. That will never work. I added a new variable called counter, and that works fine now.

I changed "void main (void)" because I don't think you can void main like that. I think it must be int, I could be wrong.

Now, if you have the formula's, you should be able to do the rest. I hope this helps you out a little bit.

#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
     double counter;
     double x;
     double max;
     double min=100;
     double sum;
     double mean;
     double var;
     double std_dev;
     double total = 0;

     printf("Please enter a series of numbers terminated by -1: ");
     scanf("%lg", &x);

     total = total + x;
     while(x > 0)
     {
       if (x > max)
         max = x;

       else if (x < min)
         min = x;

     printf("Enter another number: ");
     scanf("%lg", &x);
     counter++;
     };


       cout << "You entered " << counter << " values.\n" <<endl;
       cout << "Maximum value entered was " << max << endl;
       cout << "Minimum number entered was " << min << endl;
     

     printf("The program ran successfully");


     printf("\n\n");
     system("PAUSE");
     return 0;
}




One further piece of advice if I may be so bold... I wouldn't wait until my homework was due the next day before I asked for help. Sometimes questions can sit for a day or two before they pick up a reply.
Was This Post Helpful? 0

#4 lordace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-May 08

Re: C program Beginner lvl

Posted 23 May 2008 - 09:45 PM

Yes I know and I am sorry for posting and needing it for the next day, the thing is that I was tryin to do as much as possible alone I got it on thursday and we got two days. I have read over 150 pages of programing trying to make sense of everything. I did not want to post hey can someone do this for me. But thank you so much you really helped out alot. you rock.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Whizzy*


Reputation:

Re: C program Beginner lvl

Posted 24 May 2008 - 07:17 AM

View Postlordace, on 23 May, 2008 - 09:45 PM, said:

you rock.


I'm a beginner, just like you. I don't rock, but I'm happy to attemp to help.
I commend you for trying to do as much as you can on your own. That's is the goal of everyone here. To teach you. So when you post your problem, simply say "I need to do this, just point in the right direction", and they will respect your wish, and "drop you some hints". This way you won't waste your time trying to read up on wrong methods and the likes. These guys here are the greatest.
Was This Post Helpful? 0

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: C program Beginner lvl

Posted 24 May 2008 - 07:26 AM

Ummmm... Whizzy, you added iostream, but the OP said that they were working with C. I just translated it to C, here it is:
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
     double counter;
     double x;
     double max;
     double min=100;
     double sum;
     double mean;
     double var;
     double std_dev;
     double total = 0;

     printf("Please enter a series of numbers terminated by -1: ");
     scanf("%lg", &x);

     total = total + x;
     while(x > 0)
     {
       if (x > max)
         max = x;

       else if (x < min)
         min = x;

     printf("Enter another number: ");
     scanf("%lg", &x);
     counter++;
     };


       printf("You entered %d values.\n", counter);
       printf("Maximum value entered was %f\n", max);
       printf("Minimum number entered was %f\n", min);


     printf("The program ran successfully");


     printf("\n\n");
     system("PAUSE");
     return 0;
}

Oh, and you were right about main, it should return an int.
The only other thing is that you had the argc and argv, which are used for command line (like when you call shutdown /s) /s is the arg being sent.

:)
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Whizzy*


Reputation:

Re: C program Beginner lvl

Posted 24 May 2008 - 08:16 AM

Ok.. I was wrong. But I took a shot...
Was This Post Helpful? 0

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: C program Beginner lvl

Posted 24 May 2008 - 08:22 AM

No problem... your advice still made sense, its just that cout is C++ standard, printf() is C standard :)

This post has been edited by gabehabe: 24 May 2008 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,119
  • Joined: 22-February 08

Re: C program Beginner lvl

Posted 24 May 2008 - 11:30 PM

cant we use printf in C++? if it's just for pure text-output it uses less memory than cout. although computer nowadays are too powerful that we really dont have to care about it
Was This Post Helpful? 0
  • +
  • -

#10 gondal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-May 08

Re: C program Beginner lvl

Posted 25 May 2008 - 12:41 AM

View Postred_4900, on 24 May, 2008 - 11:30 PM, said:

cant we use printf in C++? if it's just for pure text-output it uses less memory than cout. although computer nowadays are too powerful that we really dont have to care about it



cin and cout are liib funcs of iostream

so u need to include iostream to use them

which of course takes up mem

i prerfer going to the very limits when i say i am going to conserve mem

it feels nice to push the limnits even now a days mems are way too big than how they used to be
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1