# Clearing all elements in array and displaying the sum of numbers in an array!

This is a discussion on Clearing all elements in array and displaying the sum of numbers in an array! within the C Programming forums, part of the General Programming Boards category; Hey, I have created this program that displays a menu and asks the user to select their option and program ...

1. ## Clearing all elements in array and displaying the sum of numbers in an array!

Hey, I have created this program that displays a menu and asks the user to select their option and program will do that option. I tried to make my code as easy to understand but if you need help understanding please ask me.

Code:
```char user1; // Variable for users menu option selection
int totalCounter = 0; // counter for how many times 'E' is executed
int array[10]; // Array for the user inputted number
int positiveCount; // variable for how many times positive numbers were entered
int positiveArray[10]; // array for storing the positive numbers
int positiveSum; // variable for calculating the sum of the positive numbers
int negativeCount; // variable for how many time negative numbers were entered
int negativeSum; // variable for the sum of the negative numbers
int negativeArray[10]; // array for storing the negative numbers
int j=0; // used for storing integers into the array ('array')

int main(void)
{
printf ("\n*======================================================*"
"\nE (e): Enter a new number                                *" // prompts the user to enter a number
"\nD (d): Display all the numbers                           *" // displays all the numbers
"\nP (p): Display all positive numbers and statistics       *" // displays all positive numbers and its total count and sum
"\nN (n): Display all negative numbers and statistics       *" // displays all negative numbers and its total count and sum
"\nC (c): Clear all data in the memory                      *" // clears all the elements in the 'array'
"\nQ (q): Quit Program                                      *" // exists the program
"\n*========================================================*"
"\n*Enter your choice: "); // promps the user to enter a selection
scanf (" %c", &user1); // saves it to 'user1'
user1 = toupper(user1); // Turns all the user inputs for the menu into to upper case

while ( j != 10 ) // Need to loop until all the elements in the variable 'array' are filled (not working)
{
switch (user1)
{
case 'E':
printf ("\nPlease enter an integer number: "); // asks to enter an integer
scanf ("%d", &array[j]); // saves it to the variable 'array' in the 'j' index (set to 0)
j++; // j is increased by one
totalCounter++; // adds one to the total counter
break;
case 'D':
printf ("\nSo far these are the numbers you entered: "); // prints all the numbers entered
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
printf ("%d, ", array[i]); // prints all the numbers in each of the index's at the array 'array'
}
printf ("\n\nTotal numbers: %d\n", totalCounter); // prints the total count of the numbers
break;
case 'P':
printf ("\nThese are all the positive numbers you entered: "); // prints all the positive numbers
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
if ( array[i] > 0 ) // checks for positive numbers in the index's in 'array'
{
printf ("%d, ",array[i]); // prints them
positiveCount++; // increases the positive count by 1
positiveArray[i] = array[i]; // saves all the positive numbers in 'array' to 'positiveArray'
}
}
for ( int i=0; i < 10; i++ ) // does the loop for 10 times because 10 integers in the 'positiveArray'
{
positiveSum = positiveSum + positiveArray[i]; // positiveSum is calculated (not working after one loop)
}
printf ("\n\nTotal Numbers: %d\n\n", positiveCount); // displays total positive count
printf ("The total sum is: %d\n", positiveSum); // displays total positive sum
break;
case 'N':
printf ("\nThese are all the negative numbers you entered: "); // prints all the negative numbers
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
if ( array[i] < 0 ) // checks for negative numbers in the index's in 'array'
{
printf ("%d, ",array[i]); // prints them
negativeCount++; // increases the negative count by 1
negativeArray[i] = array[i]; // saves all the negative numbers in 'array' to 'negativeArray'
}
}
for ( int i=0; i < 10; i++ ) // does the loop for 10 times because 10 integers in the 'negaiveArray'
{
negativeSum = negativeSum + negativeArray[i]; // negativeSum is calculated (not working after one loop)
}
printf ("\n\nTotal Numbers: %d\n\n", negativeCount); // displays total negative count
printf ("The total sum is: %d\n", negativeSum); // displays total negative sum
break;
case 'C':
printf ("\nClearing all data in the memory!!\n"); // clears all data in the elements of the main 'array'
j = 0; // by making j=0 (does not work)
printf ("\nDone!\n"); // prints done
break;
case 'Q':
exit(1); // exists if 'user1' is equal to q
break;

}
printf ("\n*Enter your choice: "); // loops again by asking another choice
scanf (" %c", &user1); // save to 'user1'
user1 = toupper(user1); // makes it uppercase
}
system("PAUSE");
return 0;
}```
It works fine but there are 3 problems that occur after looping the program first..

1) The positive and negative sum only works if I do them in order so if I choose option 'E' two times in a row and then press 'P' or 'N' it will work and show me the total sum and the numbers but if i press 'E' two times in a row and then press 'P' or 'N' and then press 'E' again and then press 'P' or 'N' again, it will show me the wrong sum and the wrong count of the numbers.

2) I am trying to loop this program until j doesn't equal to 10 which is the same as while 'array' is not upto its [10] index, the problem is I can keep looping the program even after j=10 or in other words I can keep looping even after I used all the 10 elements in 'array'.

3) In option 'C', I tried to clear the memory of 'array' by setting j=0 which will clear all the elements in 'array' but that does not work. How can I clear all the elements in 'array'?

I hope someone can help me. Thanks a lot in advance.

2. I expect that your first problem is related to your use of global variables that, although they have at least been initialized (this is good), are not properly updated throughout the code. For example, check that you reset your counter to 0 every time you loop through the array to count something.

In general, you don't "clear" the elements of an array unless by "clear" you mean setting every element to 0. You can, however, use a variable to record the logical "length" of the array and set that to 0, indicating a logically empty array.

It also appears that the variable j in your code is used for this purpose, but there is some confusion in the code that loops over the array where you are trying to use a different variable for this purpose.

In general, you'd be a lot better off rewriting this program so that each menu item becomes a different function, passing the array and other needed variables to those functions. This will eliminate the global variables and probably most of your problems.

Some of the comments are good, such as those that describe the purpose of the variables, but some are unnecessary such as
Code:
` j++; // j is increased by one`
. First, that is quite obvious to any C programmer and it adds to the "denseness" factor of the code, i.e., how much there is to read on every line. And in actuality, it is NOT true. If j == INT_MAX, then j is not increased by one.

3. Hey, sorry for the late reply. I did what you said like make all my counters reset to 0 and that worked like a charm. Only question I have now is that how can I make this while loop only run until all the index's in 'array' is used up? I looked through my book and the only helpful thing I found was to use a for loop and checking in each index but what do they mean by checking each index?

EDIT: Yes, I am rewriting my whole program to make it neater and to get rid of most my problems. Except the one that I still have. ^^