How Can You Efficiently Count Age Groups in C Programming?

In summary: Here's a summary of the conversation:In summary, the assignment is to count the number of people in different age groups and report the total for each group. The solution provided uses six integer variables and if/else if statements to increment the count for each group based on the age entered by the user. The code also includes a while loop to continuously prompt for ages until the user enters -1 to quit. The code should be properly indented and the lines for prompting and reading input should be moved inside the loop. Additionally, the code is not compiling because the closing curly brace for the while loop is missing.
  • #1
clook
35
0
the assignment wants me to count the number of people in age groups and to report the number of people in each group.

i came up with this, used an int for each group and incremented with if/else if statements.

Code:
#include<stdio.h>
main()
{
      int x, infant, young, middleaged, old, older, reallyold;
      infant = 0;
      young = 0;
      middleaged = 0;
      old = 0;
      older = 0;
      reallyold = 0;
      printf("Please enter ages (Enter -1 to quit): ");
      scanf("%d", x);
	  while (x != -1)
    {
      if (x <= 16)
      infant++;
      else if (x <= 29)
      young++;
      else if (x <= 55)
      middleaged++;
      else if (x <= 75)
      old++;
      else if (x <= 99)
      older++;
      else if (x > 99)
      reallyold++;
      printf("Number of infants: %d \n", infant);
      printf("Number of young people: %d \n", young);
      printf("Number of middle aged people: %d \n", middleaged);
	  printf("Number of old people: %d \n", old);
	  printf("Number of older people: %d \n", older);
	  printf("Number of really old people: %d \n", reallyold);}

though I'm not sure if its exactly right.. and it won't let me compile.

i keep getting the "unexpected end of file" error. please help? :)i also want to make it so that you can enter as many ages as you want, but I'm not sure what kind of loop i should use for that.
 
Last edited:
Physics news on Phys.org
  • #2
You should move the lines
printf("Please enter ages (Enter -1 to quit): ");
scanf("%d", x);
to just inside the while loop, not just outside it. Then you'll also have to initialize x to something other than -1. The reason it's not compiling is you forgot the closing curly brace } on the while loop.

Also you should use proper indentation. The while keyword itself should be the same indentation as the stuff before it, and the contents of the loop should be indented one more. Also if you have an "if" statement, the body of the statement should be indented one more than the "if" statement, like.
Code:
while X
{
    if A
        B
    else if C
        D
    else if G
        E
    other stuff here
}
 
  • #3


Hi there,

Thank you for reaching out for help with your computer science assignment. It seems like you are on the right track with your code, but there are a few things that I would suggest to make it more efficient and accurate.

First, instead of using individual int variables for each age group, you can use an array to store the counts for each group. This will make your code more concise and easier to read. For example:

int ages[6]; // array to store counts for each age group
ages[0] = 0; // index 0 represents infants
ages[1] = 0; // index 1 represents young people
ages[2] = 0; // index 2 represents middle aged people
ages[3] = 0; // index 3 represents old people
ages[4] = 0; // index 4 represents older people
ages[5] = 0; // index 5 represents really old people

Next, instead of using multiple if/else if statements, you can use a for loop to iterate through the array and increment the appropriate index based on the age entered by the user. For example:

printf("Please enter ages (Enter -1 to quit): ");
scanf("%d", x);

while (x != -1) {
for (int i = 0; i < 6; i++) { // loop through the array
if (x <= age_limits) { // age_limits is an array containing the upper limits for each age group
ages++; // increment the count for the appropriate age group
break; // exit the for loop once the age group has been determined
}
}

printf("Please enter ages (Enter -1 to quit): ");
scanf("%d", x);
}

Finally, to avoid the "unexpected end of file" error, make sure that you have closed all curly brackets and ended all statements with a semicolon.

I hope this helps and good luck with your assignment!
 

Related to How Can You Efficiently Count Age Groups in C Programming?

1. What is the purpose of the "Need help with compsci assignment (C)" assignment?

The purpose of this assignment is to test your understanding and application of concepts in the C programming language. It is designed to challenge your problem-solving skills and improve your programming abilities.

2. How long is the "Need help with compsci assignment (C)" assignment?

The length of the assignment will vary depending on the specific requirements and your level of proficiency in C. It is important to carefully read and understand the instructions before starting the assignment and to allocate enough time for completion.

3. Can I collaborate with others on the "Need help with compsci assignment (C)" assignment?

It is generally not permitted to collaborate with others on programming assignments, as it is important for you to demonstrate your individual understanding and skills. However, you may seek help from your instructor or classmates if you encounter difficulties or have questions.

4. What resources are available to help me with the "Need help with compsci assignment (C)" assignment?

There are various resources you can use to help you with this assignment, such as textbooks, online tutorials, and programming forums. Additionally, your instructor or teaching assistant may be able to provide guidance and clarification on specific concepts.

5. What should I do if I am unable to complete the "Need help with compsci assignment (C)" assignment?

If you are struggling with the assignment, it is important to communicate with your instructor as soon as possible. They may be able to provide additional resources or offer extensions if necessary. It is also important to review and understand any feedback provided on previous assignments to improve your understanding for future assignments.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
917
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top