2 Questions about my array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gator6688
    New Member
    • Sep 2007
    • 63

    2 Questions about my array.

    I have to make a program that enters either a 1, 2, 3, or 4 for 50 people attending a function. Then I have to be able to count how many of each 1's, 2's ,3's, and 4's there were. My 2 questions are:

    1.) Would this program be better as a while loop so it is continous or a for loop?

    2.) How could I start code to count each individual category?



    Code:
    #include "stdafx.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const int People = 5;
    	int i, PeopleTypes[People], count=0;
    
    cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
    cout << "for each person that attended the school function.\n\n";
    
    	for(i=1; i<People; i++)
    	{
    		cout << "Person #" << i << ": ";
    		cin  >> PeopleTypes[i];
    	if (PeopleTypes[i]>4 || PeopleTypes[i]<1)
    	{
    	cout << "This is invalid input!\n\n";
    	
    	}
    	if (PeopleTypes[i]<0)
    		break;
    	}
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by gator6688
    I

    1.) Would this program be better as a while loop so it is continous or a for loop?

    2.) How could I start code to count each individual category?

    First, while loops and for loops are the same thing. Anything you can do with a for loop you can also do with a while loop and the opposite is true too. I always consider using a while loop first.

    Count how many 1s 2s 3s and 4s you have in your array you can just start at the 0th index and have four counters like, numberOfOnes, numberOfTwos, numberOfThrees. .. and increment them each time you have one of those numbers.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Originally posted by RedSon
      First, while loops and for loops are the same thing. Anything you can do with a for loop you can also do with a while loop and the opposite is true too. I always consider using a while loop first.

      Count how many 1s 2s 3s and 4s you have in your array you can just start at the 0th index and have four counters like, numberOfOnes, numberOfTwos, numberOfThrees. .. and increment them each time you have one of those numbers.
      Also, a corollary to the first statement is that, any thing that can be done in a loop can also be done with recursion. But in most cases your head explodes if you try it.

      Comment

      • gator6688
        New Member
        • Sep 2007
        • 63

        #4
        maybe something like this?

        Code:
        switch (PeopleTypes)
           {
           case 1:
        	   cout << "The total number of Infants is " << numberOfOnes;
        	   break;
           case 2:
        	   cout << "The total number of Children is " << numberOfTwos;
        	   break;
           case 3:
        	   cout << "The total number of Teenagers is " << numberOfThrees;
        	   break;
           case 4:
        	   cout << "The total number of Adults is " << numberOfFours;
        	   break;
           }

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by gator6688
          maybe something like this?

          Code:
          switch (PeopleTypes)
             {
             case 1:
          	   cout << "The total number of Infants is " << numberOfOnes;
          	   break;
             case 2:
          	   cout << "The total number of Children is " << numberOfTwos;
          	   break;
             case 3:
          	   cout << "The total number of Teenagers is " << numberOfThrees;
          	   break;
             case 4:
          	   cout << "The total number of Adults is " << numberOfFours;
          	   break;
             }
          What? No! Wait, what are you trying to accomplish? Explain to me in words what you are hoping will happen with the above code snippit.

          Comment

          • gator6688
            New Member
            • Sep 2007
            • 63

            #6
            I need the program to add up the 1's, 2's, 3's, and 4's as they are entered and then output how many of each were entered.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by gator6688
              I need the program to add up the 1's, 2's, 3's, and 4's as they are entered and then output how many of each were entered.
              Now explain to me why you think a switch statement would help you add up the 1's 2's 3's and 4s and then help you output them.

              Comment

              • gator6688
                New Member
                • Sep 2007
                • 63

                #8
                Well, I came up with this and it works.

                Code:
                #include "stdafx.h"
                #include <iostream>
                using namespace std;
                
                int main()
                {
                	const int People = 10;
                	int i, PeopleTypes[People], numberOfOnes=0, numberOfTwos=0, numberOfThrees=0, numberOfFours=0;
                
                cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
                cout << "for each person that attended the school function.\n\n";
                
                	
                	for(i=1; i<=People; i++)
                	{
                		cout << "Person #" << i << ": ";
                		cin  >> PeopleTypes[i];
                
                		if (PeopleTypes[i]<0)
                        break;
                 
                
                switch (PeopleTypes[i])
                   {
                   case 1:
                	   ++numberOfOnes;
                	   break;
                   case 2:
                	   ++numberOfTwos;
                	   break;
                   case 3:
                	   ++numberOfThrees;
                	   break;
                   case 4:
                	   ++numberOfFours;
                	   break;
                default:
                cout << "This is invalid input!\n\n";
                   }
                	}
                	cout << "The total number of infants attending is " << numberOfOnes << endl;
                	cout << "The total number of children attending is " << numberOfTwos << endl;
                	cout << "The total number of teenagers attending is " << numberOfThrees << endl;
                	cout << "The total number of adults attending is " << numberOfFours << endl;
                	cout << endl << endl;
                	
                	
                   
                	
                }

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Originally posted by gator6688
                  Well, I came up with this and it works.

                  Code:
                  #include "stdafx.h"
                  #include <iostream>
                  using namespace std;
                  
                  int main()
                  {
                  	const int People = 10;
                  	int i, PeopleTypes[People], numberOfOnes=0, numberOfTwos=0, numberOfThrees=0, numberOfFours=0;
                  
                  cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
                  cout << "for each person that attended the school function.\n\n";
                  
                  	
                  	for(i=1; i<=People; i++)
                  	{
                  		cout << "Person #" << i << ": ";
                  		cin  >> PeopleTypes[i];
                  
                  		if (PeopleTypes[i]<0)
                          break;
                   
                  
                  switch (PeopleTypes[i])
                     {
                     case 1:
                  	   ++numberOfOnes;
                  	   break;
                     case 2:
                  	   ++numberOfTwos;
                  	   break;
                     case 3:
                  	   ++numberOfThrees;
                  	   break;
                     case 4:
                  	   ++numberOfFours;
                  	   break;
                  default:
                  cout << "This is invalid input!\n\n";
                     }
                  	}
                  	cout << "The total number of infants attending is " << numberOfOnes << endl;
                  	cout << "The total number of children attending is " << numberOfTwos << endl;
                  	cout << "The total number of teenagers attending is " << numberOfThrees << endl;
                  	cout << "The total number of adults attending is " << numberOfFours << endl;
                  	cout << endl << endl;
                  	
                  	
                     
                  	
                  }
                  Ok, good .

                  Comment

                  Working...