473,387 Members | 1,553 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,387 software developers and data experts.

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?



Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. using namespace std;
  3.  
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6.     const int People = 5;
  7.     int i, PeopleTypes[People], count=0;
  8.  
  9. cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
  10. cout << "for each person that attended the school function.\n\n";
  11.  
  12.     for(i=1; i<People; i++)
  13.     {
  14.         cout << "Person #" << i << ": ";
  15.         cin  >> PeopleTypes[i];
  16.     if (PeopleTypes[i]>4 || PeopleTypes[i]<1)
  17.     {
  18.     cout << "This is invalid input!\n\n";
  19.  
  20.     }
  21.     if (PeopleTypes[i]<0)
  22.         break;
  23.     }
Oct 19 '07 #1
8 1427
RedSon
5,000 Expert 4TB
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.
Oct 19 '07 #2
RedSon
5,000 Expert 4TB
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.
Oct 19 '07 #3
maybe something like this?

Expand|Select|Wrap|Line Numbers
  1. switch (PeopleTypes)
  2.    {
  3.    case 1:
  4.        cout << "The total number of Infants is " << numberOfOnes;
  5.        break;
  6.    case 2:
  7.        cout << "The total number of Children is " << numberOfTwos;
  8.        break;
  9.    case 3:
  10.        cout << "The total number of Teenagers is " << numberOfThrees;
  11.        break;
  12.    case 4:
  13.        cout << "The total number of Adults is " << numberOfFours;
  14.        break;
  15.    }
Oct 19 '07 #4
RedSon
5,000 Expert 4TB
maybe something like this?

Expand|Select|Wrap|Line Numbers
  1. switch (PeopleTypes)
  2.    {
  3.    case 1:
  4.        cout << "The total number of Infants is " << numberOfOnes;
  5.        break;
  6.    case 2:
  7.        cout << "The total number of Children is " << numberOfTwos;
  8.        break;
  9.    case 3:
  10.        cout << "The total number of Teenagers is " << numberOfThrees;
  11.        break;
  12.    case 4:
  13.        cout << "The total number of Adults is " << numberOfFours;
  14.        break;
  15.    }
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.
Oct 19 '07 #5
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.
Oct 19 '07 #6
RedSon
5,000 Expert 4TB
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.
Oct 19 '07 #7
Well, I came up with this and it works.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     const int People = 10;
  8.     int i, PeopleTypes[People], numberOfOnes=0, numberOfTwos=0, numberOfThrees=0, numberOfFours=0;
  9.  
  10. cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
  11. cout << "for each person that attended the school function.\n\n";
  12.  
  13.  
  14.     for(i=1; i<=People; i++)
  15.     {
  16.         cout << "Person #" << i << ": ";
  17.         cin  >> PeopleTypes[i];
  18.  
  19.         if (PeopleTypes[i]<0)
  20.         break;
  21.  
  22.  
  23. switch (PeopleTypes[i])
  24.    {
  25.    case 1:
  26.        ++numberOfOnes;
  27.        break;
  28.    case 2:
  29.        ++numberOfTwos;
  30.        break;
  31.    case 3:
  32.        ++numberOfThrees;
  33.        break;
  34.    case 4:
  35.        ++numberOfFours;
  36.        break;
  37. default:
  38. cout << "This is invalid input!\n\n";
  39.    }
  40.     }
  41.     cout << "The total number of infants attending is " << numberOfOnes << endl;
  42.     cout << "The total number of children attending is " << numberOfTwos << endl;
  43.     cout << "The total number of teenagers attending is " << numberOfThrees << endl;
  44.     cout << "The total number of adults attending is " << numberOfFours << endl;
  45.     cout << endl << endl;
  46.  
  47.  
  48.  
  49.  
  50. }
Oct 19 '07 #8
RedSon
5,000 Expert 4TB
Well, I came up with this and it works.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     const int People = 10;
  8.     int i, PeopleTypes[People], numberOfOnes=0, numberOfTwos=0, numberOfThrees=0, numberOfFours=0;
  9.  
  10. cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
  11. cout << "for each person that attended the school function.\n\n";
  12.  
  13.  
  14.     for(i=1; i<=People; i++)
  15.     {
  16.         cout << "Person #" << i << ": ";
  17.         cin  >> PeopleTypes[i];
  18.  
  19.         if (PeopleTypes[i]<0)
  20.         break;
  21.  
  22.  
  23. switch (PeopleTypes[i])
  24.    {
  25.    case 1:
  26.        ++numberOfOnes;
  27.        break;
  28.    case 2:
  29.        ++numberOfTwos;
  30.        break;
  31.    case 3:
  32.        ++numberOfThrees;
  33.        break;
  34.    case 4:
  35.        ++numberOfFours;
  36.        break;
  37. default:
  38. cout << "This is invalid input!\n\n";
  39.    }
  40.     }
  41.     cout << "The total number of infants attending is " << numberOfOnes << endl;
  42.     cout << "The total number of children attending is " << numberOfTwos << endl;
  43.     cout << "The total number of teenagers attending is " << numberOfThrees << endl;
  44.     cout << "The total number of adults attending is " << numberOfFours << endl;
  45.     cout << endl << endl;
  46.  
  47.  
  48.  
  49.  
  50. }
Ok, good .
Oct 19 '07 #9

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

Similar topics

27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
6
by: Last Timer | last post by:
I need help with the following questions: a) if a class has virtual functions, is it better to make the destructor virtual b) can a friend function be inherited c) if a function argument is...
4
by: William | last post by:
I would appreciate your help on the following programming questions: 1. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates. HINT: The...
3
by: sieg1974 | last post by:
Hi, I have made this simple program to understand char ** pointers, but I still having many questions. int main() { char ** testPointerPointerChar = 0; char * A = "string01";
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
27
by: fctk | last post by:
hello, i have some questions. 1) do constant expressions include string literals? for example, is "hello, world" a constant expression? 2) int i = 0; is the equal sign the assignment...
1
by: nono909 | last post by:
i need answers for the following questions so i'll be prepaed for my next exam.please Q1. Write the copy constructor for a dynamic array class template. is the following answer right? template...
12
by: Gilbert | last post by:
H, i'm starting with asp.net/vb.net and have some questions about arrays and collections: 1) what's the difference between: dim x() as string and dim x as array
16
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window to dispaly a set of questions with their answers. The users should be able to move to the next question by clicking on next button. I am going to use only one panel...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.