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? - #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;
-
}
8 1427
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.
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.
maybe something like this? - 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;
-
}
maybe something like this? - 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.
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.
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.
Well, I came up with this and it works. - #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;
-
-
-
-
-
}
Well, I came up with this and it works. - #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 .
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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";
|
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...
|
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;
|
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...
|
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...
|
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
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |