-5

So I'm trying to get a simple program to work by asking the user to enter numbers within 1-100 and have the program store everything entered then tell the user later on all the numbers and how much it adds up to be.

Right now all I want to know is how do I store the variables and have the program be able to tell me how many numbers were entered.

Should I make a function outside of main that does the processing for storing and adding?

#include <stdio.h>




int main () {

int number, even, odd;

char name;


printf("Enter your name")
scanf(%d, &number);
scanf (%c, &char)

printf("Enter numbers within 1-100")
printf("Enter 0 to quit")

while (number != 0) {
    if (number%2 == 1) {
        //This is where I don't know how to store the odd numbers
    }
    else {

        //And the even numbers here as well
    }



}



printf("%c,the numbers you have entered are broken down as follows:\n",name);
printf("You entered %d even numbers with a total value of \n", even);
printf("You entered %d odd numbers with a total value of \n", odd);




return 0;

}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Jclee
  • 39
  • 9
  • I also need to make a variable for total and have that reflect for the output at the end, but I'm not worried about that just yet – Jclee Feb 18 '16 at 03:50
  • Use integer or double to accumulate the value, but you don't seem to get the user input either in the program. Where does `number` come from? – Ian Feb 18 '16 at 03:51
  • 1
    Do you need to store everything or just the number of items entered and the running sum? I don't quite get your question - seems like you are basically saying "I don't understand variables"... – John3136 Feb 18 '16 at 03:52
  • Sorry, forgot to add the scanf after asking for number. And you're halfway correct, I don't know the full functions of variables. I want the program to count how many numbers were entered that were in the even category as well as the odd, then the sum of all of the numbers in both categories. Basically, how do i make the program know how to store all the different entries being added, I'm guessing adding all the entries together will be easy to get after I find that out. – Jclee Feb 18 '16 at 03:59
  • 3
    You seem to lack basic C knowledge. I suggest you read a good book/tutorial before attempting to solve this problem. – Spikatrix Feb 18 '16 at 04:24

3 Answers3

0

Your question is a little ambiguous but it seems to me that you may need to allocate memory dynamically for storage since you don't know how many numbers will be entered during run time.

This can be done multiple ways. The first method involves using the malloc() and realloc() functions. I'll leave you to research these functions but basically malloc() allocates memory on the heap during run time and realloc() allows you to resize the memory that was given to you.

I think the best method would be to implement a linked list data structure. This way you can store the numbers as the user enters them and then later iterate through the list and count how many numbers were odd or even. And similarly you can also calculate their totals.

More information on linked list data structures:

These are just some of the resources I used to learn linked lists. There are plenty more information about linked lists on Google.

MatthewT53
  • 109
  • 5
0

Here is a sample program. Enhance as required as suggested in the previous update.

#include <stdio.h>
#include <stdlib.h>
int
main ()
{

  int number, even, odd;
  int *evenarr, *oddarr;
  int evencount = 0, oddcount = 0;
  int i;

  char name;

  evenarr = (int *) malloc(100 * sizeof(int));
  oddarr = (int *) malloc(100 * sizeof(int));

  printf ("Enter numbers within 1-100");
  printf ("Enter 0 to quit");
  do
    {

      scanf ("%d", &number);

      if (number != 0)
        {
          if (number < 1 || number > 100)
            continue;
          if (number % 2 == 1)
            {
              //This is where I don't know how to store the odd numbers
              printf ("odd\n");
              oddarr[oddcount] = number;
              oddcount++;
              /* Realloc the arr if size exceed 100 */
            }
          else
            {

              //And the even numbers here as well
              printf ("even\n");
              evenarr[evencount] = number;
              evencount++;
              /* Realloc the arr if size exceed 100 */
            }
        }
    }
  while (number != 0);

  for(i=0; i<oddcount; i++)
    printf("odd : %d\n", oddarr[i]);
  for(i=0; i<evencount; i++)
    printf("even : %d\n", evenarr[i]);

}
Umamahesh P
  • 1,224
  • 10
  • 14
0

You can use malloc(). It allocates memory on the heap. Also, using realloc() allows you to resize the memory you allocated using malloc().

See this tutorial on malloc and free.

The code @Umamahesh has given is correct, just that he is not freeing the memory allocated by the program, which might be fatal.

So you just free the pointers. The corrected code in @Umamahesh answer is:

#include <stdio.h>
#include <stdlib.h>
int
main ()
{

  int number, even, odd;
  int *evenarr, *oddarr;
  int evencount = 0, oddcount = 0;
  int i;

  char name;

  evenarr = (int *) malloc(100 * sizeof(int));
  oddarr = (int *) malloc(100 * sizeof(int));

  printf ("Enter numbers within 1-100");
  printf ("Enter 0 to quit");
  do
    {

      scanf ("%d", &number);

      if (number != 0)
        {
          if (number < 1 || number > 100)
            continue;
          if (number % 2 == 1)
            {
              //This is where I don't know how to store the odd numbers
              printf ("odd\n");
              oddarr[oddcount] = number;
              oddcount++;
              /* Realloc the arr if size exceed 100 */
            }
          else
            {

              //And the even numbers here as well
              printf ("even\n");
              evenarr[evencount] = number;
              evencount++;
              /* Realloc the arr if size exceed 100 */
            }
        }
    }
  while (number != 0);

  for(i=0; i<oddcount; i++)
    printf("odd : %d\n", oddarr[i]);
  for(i=0; i<evencount; i++)
    printf("even : %d\n", evenarr[i]);
  free (evenarr);
  free (oddarr);

}

Also see: How do free and malloc work in C?.

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67