i NEED help in c++

for the life of me i cannot find out how to do this!

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

47 Replies - 4418 Views - Last Post: 27 May 2009 - 06:10 AM Rate Topic: -----

#1 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

i NEED help in c++

Post icon  Posted 24 April 2009 - 10:40 AM

in my class im supposed to let the user enter 50 numbers with string ( using
spaces ) then do 5 different things with those numbers; determine the maximum, average, range, and mode and then make a histogram ( which i already did that one ) this is everything in my program so far.

#include<iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
	// delare variables
	float r[50];
	float all=0;
	float average = 0;
	string c1;
	string c2;
	string c3;
	string c4;
	string c5;
	string c6;
	string c7;
	string c8;
	string c9;
	string c10;

	// in this for loop im using string to show the amount of times 
	// a number within a group of numbers apeared using a table.
	for (int c=0; c<=50; c++)
	{
		// randomization
		r[c]= rand() % 50;

		// nums 1-5
		if ( r[c]>0 && r[c]<6 )
		{
			c1 += "*";
		}
		// nums 6-10
		else if ( r[c]>5 && r[c]<11 )
		{
			c2 += "*";
		}
		// nums 11-15
		else if ( r[c]>10 && r[c]<16 )
		{
			c3 += "*";
		}
		// nums 16-20
		else if ( r[c]>15 && r[c]<21 )
		{
			c4 += "*";
		}
		// nums 21-25
		else if ( r[c]>20 && r[c]<26 )
		{
			c5 += "*";
		}
		// nums 1-30
		else if ( r[c]>25 && r[c]<31 )
		{
			c6 += "*";
		}
		// nums 31-35
		else if ( r[c]>30 && r[c]<36 )
		{
			c7 += "*";
		}
		// nums 36-40
		else if ( r[c]>35 && r[c]<41 )
		{
			c8 += "*";
		}
		// nums 41-45
		else if ( r[c]>40 && r[c]<46 )
		{
			c9 += "*";
		}
		// nums 46-50
		else
		{
			c10 += "*";
		}
	}
	cout << endl << " This is the bar graph im using to display and compare how many times a" << endl;
	cout << " number apeared in groups of 5's of 1-50.";

	// this is the output in bargraph form of all numbers 1-50 
	cout << endl << "   1-5: " << c1 << endl;
	cout << "  6-10; " << c2 << endl;
	cout << " 11-15: " << c3 << endl;
	cout << " 16-20: " << c4 << endl;
	cout << " 21-25: " << c5 << endl;
	cout << " 26-30: " << c6 << endl;
	cout << " 31-35: " << c7 << endl;
	cout << " 36-40: " << c8 << endl;
	cout << " 41-45: " << c9 << endl;
	cout << " 46-50: " << c10 << endl << endl;
	cout << " This is the average of all 50 numbers: ";
	
	
	// all numbers going into one variable
	for (c=0; c==50; c++)
	{
		all += r[c];
	}

	// average
	average = all / 50;
	cout << average << endl;

	// mode
	for (c=0; c==50; c++)
	{
		
	}
	
	
	
	
	return (0);
}



im not getting any errors but the "all" variable is apearently = to 0 and i have no idea how to do mode but i do know how to do the others. this sucks some one help me! wahhh! i really need help with mode.




















oh and i need some one to teach me how to put string in this program to. thank u!

Is This A Good Question/Topic? 0
  • +

Replies To: i NEED help in c++

#2 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 24 April 2009 - 11:16 AM

i just added this and it turns out that i need help with every thing :(


	// minimum and maximum
	for (c=0; c==50; c++)
	{
		// minimum
		if ( r[c] < min )
		{
			min = r[c];
		}
		
		// mazimum
		if ( r[c] > max )
		{
			max = r[c];
		}
	}



i need help with minimum and maximum!!!
Was This Post Helpful? 0
  • +
  • -

#3 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: i NEED help in c++

Posted 24 April 2009 - 02:59 PM

loll, details, details.

What you have
  
  // all numbers going into one variable
	for (c=0; c==50; c++)
	{
		all += r[c];
	}



A fix, maybe?
	// all numbers going into one variable
	for (c=0; c<=50; c++)  //changed == to <=
	{
		all += r[c];
	}



Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: i NEED help in c++

Posted 24 April 2009 - 06:58 PM

If the user has to enter a string of fifty numbers, why not use a stringstream and a vector?

#include <sstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  string input;
  vector<int> numbers; 
  cout << "Enter fifty numbers (Separate w/ spaces): ";
  getline(cin, input);

  stringstream ss;
  ss.str(input); // set the stream to the string of numbers

  // temp variable
  int temp;

  for(int i = 0; i < 50; ++i)
  {
	ss >> temp; // extracts a number from the stream

	numbers.push_back(temp);
  }

  /*...*/



Then add in your calculations.
Was This Post Helpful? 0
  • +
  • -

#5 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 27 April 2009 - 06:23 AM

idk what vector is. i just started c++ so i guess im a newbe. sorry

View PostNotorion, on 24 Apr, 2009 - 01:59 PM, said:

loll, details, details.

What you have
  
  // all numbers going into one variable
	for (c=0; c==50; c++)
	{
		all += r[c];
	}



A fix, maybe?
	// all numbers going into one variable
	for (c=0; c<=50; c++)  //changed == to <=
	{
		all += r[c];
	}




i'll try it. thanks
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: i NEED help in c++

Posted 27 April 2009 - 06:30 AM

Read here about vectors
http://www.cplusplus...nce/stl/vector/

You should check with your teacher as to whether you are allowed to use a vector in this homework before handing in a vector based answer.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: i NEED help in c++

Posted 27 April 2009 - 10:40 AM

You don't actually need an array to store your random numbers ...

You can just store the number of counts (for each range) in a simple array of integers ... like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

// using these 2 global variables here at 'the top' can make this program
// more general and easier to 'update'

const int MAX = 100; // for random numbers in the range 0..(MAX-1)
const int NUM = 10; // number of groups ...

int main()
{
    srand( time(NULL) );

    int group[NUM] = {0}; // initial all to 0
    int min, max;
    float sum = 0;

    // take this 'actual' value ... to start
    sum = max = min = rand()%MAX  ; // an integer in the range 0..(MAX-1)
    group[ min * NUM/MAX ] ++ ; // increment 'that group count' ..

    // start counter at 1 here ...
    for (int c=1; c<MAX; c++)
    {
        int ran_num = rand() % MAX;
        group[ ran_num*NUM/MAX ]++; // increment 'that group count' ..
        if( ran_num < min ) min = ran_num;
        if( ran_num > max ) max = ran_num;
        sum += ran_num;
    }
    
    // to find a mode value (Note: we may have several equal modes ... )
    int groupNum = 0;
    int topCount = group[0];
    for( int c=1; c<NUM; c++)
        if( group[c] > topCount ) { topCount = group[c]; groupNum = c; }

    // to see if that node was unique ...
    int occurred = 0;
    for( int c=0; c<NUM; c++)
        if( group[c] == topCount ) occurred++;


    cout <<"This bar graph shows how many times a number occurred in each"
         <<" range ...\n\n";

    for( int c=0; c<NUM; c++)
    {
        cout << "[" << setw(2) << c+1 << "] "
             << setw(2) << c*MAX/NUM << " - " << setw(2) << (c+1)*MAX/NUM -1
             << " ";
        // construct a string with the number of '*' char in the value group[c]
        string bar( group[c], '*' );
        cout << bar << endl;
    }

    cout << "\nFor " << MAX << " random values in the range (0.." << MAX-1 <<")"
         << "\nThe min value was   : " << min
         << "\nThe max value was   : " << max
         << "\nThe average was     : " << sum/MAX
         << "\nA mode was at group : " << groupNum+1 << " (i.e. the range "
         << setw(2) << groupNum*MAX/NUM << " - "
         << setw(2) << (groupNum+1)*MAX/NUM -1 << ") with "
         << group[groupNum] << " counts"
         << "\nand occurred a total of " << occurred << " time(s)."

         << "\n\nPress 'Enter' to continue ... " << flush;
    cin.get();
    return 0;
}



But ... if you really need to store each random number generated, since you know in advance how many numbers ... you could use a global constant to hold that number ... like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

// using these 2 global variables here at 'the top' can make this program
// more general and easier to 'update'

const int MAX = 50; // for random numbers in the range 0..(MAX-1)
const int NUM = 10; // number of groups ...

int main()
{
    srand( time(NULL) );

    int nums[MAX];
    int group[NUM] = {0}; // initial all to 0
    int min, max;
    float sum = 0;

    // will generate and file array with random values in the range 0..(MAX-1)

    // take this 'actual' value ... to start
    sum = max = min = nums[0] = rand()%MAX  ; // an integer in the range 0..(MAX-1)
    group[ nums[0]*NUM/MAX ] ++ ; // increment 'that group count' ..

    // start counter at 1 here ...
    for (int c=1; c<MAX; c++)
    {
        nums[c] = rand() % MAX;
        group[ nums[c]*NUM/MAX ]++; // increment 'that group count' ..
        if( nums[c] < min ) min = nums[c];
        if( nums[c] > max ) max = nums[c];
        sum += nums[c];
    }
    // to find a mode value (Note: we here may have several equal modes ... )
    int groupNum = 0;
    int topCount = group[0];
    for( int c=1; c<NUM; c++)
        if( group[c] > topCount ) { topCount = group[c]; groupNum = c; }

    // to see if that node was unique ...
    int occurred = 0;
    for( int c=0; c<NUM; c++)
        if( group[c] == topCount ) occurred++;


    cout <<"This bar graph shows how many times a number occurred in each"
         <<" range ...\n\n";

    for( int c=0; c<NUM; c++)
    {
        cout << "[" << setw(2) << c+1 << "] "
             << setw(2) << c*MAX/NUM << " - " << setw(2) << (c+1)*MAX/NUM -1
             << " ";
        // construct a string with the number of '*' char in the value group[c]
        string line( group[c], '*' ); 
        cout << line << endl;
    }

    cout << "\nFor " << MAX << " random values in the range (0.." << MAX-1 <<")"
         << "\nThe min value was   : " << min
         << "\nThe max value was   : " << max
         << "\nThe average was     : " << sum/MAX
         << "\nA mode was at group : " << groupNum+1 << " (i.e. the range "
         << setw(2) << groupNum*MAX/NUM << " - "
         << setw(2) << (groupNum+1)*MAX/NUM -1 << ") with "
         << group[groupNum] << " counts"
         << "\nand occurred a total of " << occurred << " time(s)."

         << "\n\nPress 'Enter' to continue ... " << flush;
    cin.get();
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 27 April 2009 - 11:13 AM

wow. that is alot of program. well first of all i have not even learned
#include <iomanip>


and second... YOU GUYS ARE TOO ADVANCED!! i dont know what a global constant is or what a 'void' loop is or does or what ever! ( i know that u didn't use 1 but still ) :crazy: can some one help me only using
#include<iostream>
#include <cstdlib>
#include <string>
using namespace std;


all i need to do is determine the maximum, average, range, and mode. (the histogram is easy enough) i dont know how to any of those 4 things!!! this sucks!
Was This Post Helpful? 0
  • +
  • -

#9 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 27 April 2009 - 11:29 AM

the most challangeing part for me is the mode part. my teacher says that the mode hardest part. and i dont think i've metioned this but i need help with turning this prgram into a string input ( seperated by spaces ) instead of a random input. i changed my program a very little bit, i dont need an entire explaination just a refresher course
#include<iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
	// delare variables
	float r[50];
	float all=0;
	float average = 0;
	float min=0;
	float max=0;
	string c1;
	string c2;
	string c3;
	string c4;
	string c5;
	string c6;
	string c7;
	string c8;
	string c9;
	string c10;

	// in this for loop im using string to show the amount of times 
	// a number within a group of numbers apeared using a table.
	for (int c=0; c<=50; c++)
	{
		// randomization
		r[c]= rand() % 50;

		// nums 1-5
		if ( r[c]>0 && r[c]<6 )
		{
			c1 += "*";
		}
		// nums 6-10
		else if ( r[c]>5 && r[c]<11 )
		{
			c2 += "*";
		}
		// nums 11-15
		else if ( r[c]>10 && r[c]<16 )
		{
			c3 += "*";
		}
		// nums 16-20
		else if ( r[c]>15 && r[c]<21 )
		{
			c4 += "*";
		}
		// nums 21-25
		else if ( r[c]>20 && r[c]<26 )
		{
			c5 += "*";
		}
		// nums 1-30
		else if ( r[c]>25 && r[c]<31 )
		{
			c6 += "*";
		}
		// nums 31-35
		else if ( r[c]>30 && r[c]<36 )
		{
			c7 += "*";
		}
		// nums 36-40
		else if ( r[c]>35 && r[c]<41 )
		{
			c8 += "*";
		}
		// nums 41-45
		else if ( r[c]>40 && r[c]<46 )
		{
			c9 += "*";
		}
		// nums 46-50
		else
		{
			c10 += "*";
		}
	}
	cout << endl << " This is the bar graph im using to display and compare how many times a" << endl;
	cout << " number apeared in groups of 5's of 1-50.";

	// this is the output in bargraph form of all numbers 1-50 
	cout << endl << "   1-5: " << c1 << endl;
	cout << "  6-10; " << c2 << endl;
	cout << " 11-15: " << c3 << endl;
	cout << " 16-20: " << c4 << endl;
	cout << " 21-25: " << c5 << endl;
	cout << " 26-30: " << c6 << endl;
	cout << " 31-35: " << c7 << endl;
	cout << " 36-40: " << c8 << endl;
	cout << " 41-45: " << c9 << endl;
	cout << " 46-50: " << c10 << endl << endl;
	cout << " This is the average of all 50 numbers: ";
	
	
	// all numbers going into one variable
	for (c=0; c<=50; c++)
	{
		all = r[c];
	}

	// average
	average = all / 50;
	cout << average << endl;

	// minimum and maximum
	for (c=0; c<=50; c++)
	{
		// minimum
		if ( r[c] < min )
		{
			min == r[c];
		}
		
		// mazimum
		if ( r[c] > max )
		{
			max == r[c];
		}
	}
cout << min << endl << max << endl << max-min << endl;
	
	
	
	
	return (0);
}


and

Quote

loll, details, details.

What you have
  // all numbers going into one variable
	for (c=0; c==50; c++)
	{
		all += r[c];
	}



A fix, maybe?
	// all numbers going into one variable
	for (c=0; c<=50; c++)  //changed == to <=
	{
		all += r[c];
	}


does not work! still 'all = 0'! :angry:
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: i NEED help in c++

Posted 27 April 2009 - 11:38 AM

You will need to learn to use "iomanip" for any kind of programmed neat formatting ... but for now ... just take it away and the setw( ) calls it allows

And just take the 'global constant variables' away ... and use the not preferred 'magic' numbers ... like this:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    srand( time(NULL) );

    int num[50];
    int group[10] = {0}; // initial all to 0
    int min, max;
    float sum = 0;

    // take this 'actual' value ... to start
    sum = max = min = num[0] = rand()%50  ; // an integer in the range 0..(50-1)
    group[ num[0]*10/50 ] ++ ; // increment 'that group count' ..

    // start counter at 1 here ...
    for (int c=1; c<50; c++)
    {
        num[c] = rand() % 50;
        group[ num[c]*10/50 ]++; // increment 'that group count' ..
        if( num[c] < min ) min = num[c];
        if( num[c] > 50 ) max = num[c];
        sum += num[c];
    }
    // to find a mode value (Note: we here may have several equal modes ... )
    int groupNum = 0;
    int topCount = group[0];
    for( int c=1; c<10; c++)
        if( group[c] > topCount ) { topCount = group[c]; groupNum = c; }

    // to see if that node was unique ...
    int occurred = 0;
    for( int c=0; c<10; c++)
        if( group[c] == topCount ) occurred++;


    cout <<"This bar graph shows how many times a numberr occurred in each"
         <<" range ...\n\n";

    for( int c=0; c<10; c++)
    {
        cout << "[" << c+1 << "] "
             << c*50/10 << " - " << (c+1)*50/10 -1
             << " ";
        // construct a string with the number of '*' char in the value group[c]
        string line( group[c], '*' ); 
        cout << line << endl;
    }

    cout << "\nFor " << 50 << " random values in the range (0.." << 50-1 <<")"
         << "\nThe min value was   : " << min
         << "\nThe max value was   : " << max
         << "\nThe average was     : " << sum/max
         << "\nA mode was at group : " << groupNum+1 << " (i.e. the range "
         << groupNum*50/10 << " - "
         << (groupNum+1)*50/10 -1 << ") with "
         << group[groupNum] << " counts"
         << "\nand occurred a total of " << occurred << " time(s)."

         << "\n\nPress 'Enter' to continue ... " << flush;
    cin.get();
    return 0;
}

This post has been edited by David W: 27 April 2009 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 27 April 2009 - 11:43 AM

i will reply on the 30th i only do this at my school.
Was This Post Helpful? 0
  • +
  • -

#12 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 30 April 2009 - 10:05 AM

im back... let the haedaches begin!
Was This Post Helpful? 0
  • +
  • -

#13 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 30 April 2009 - 10:10 AM

what is this?
   srand( time(NULL) );


the compiler says its an error.
Was This Post Helpful? 0
  • +
  • -

#14 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 30 April 2009 - 10:34 AM

ok. i found out what my stupid problem was with the all varable:
	// all numbers going into one variable
	for (c=0; c<50; c++)
	{
		all = all + r[c];
	}

	// average
	average = all / 50;
	cout << average << endl;



i also found out that i need to use this thing called "atoi" any one know what this is????????
Was This Post Helpful? 0
  • +
  • -

#15 moo-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-April 09

Re: i NEED help in c++

Posted 30 April 2009 - 11:00 AM

i didn't think of it but i asked my teacher and i can use the 'iomanip' #include thingy. i don't know anything about it though.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »