Test array to see if it is empty.

Pages: 12
I need help with my array if it is empty it is supposed to display the words error. Can someone lead me in the correct direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 'A':
case 'a':
sum = 0;
avg = 0;
if(aArray[i]!=NULL)
     {
         cout << "Error" << endl;
         break;
       }
            else
            {
                for (int j = 0; j < i; j++)
                    {
                    sum = sum + aArray[j];
                    avg = (sum/i);
                    }

cout << endl;
cout <<"\n The average of array is: " << avg << endl;
cout << endl;
break;
            }
Last edited on
Do you want to display "Error" if an index is empty or if the whole array is empty?
Input: The letter A.
Output: The output is the AVERAGE of all the input numbers read so far, calculated as a double number. For example, if there have been four input numbers 2, 2, 6 and 5, then the average is 3.75. If there has not yet been any input numbers, then the program should print the word "ERROR" instead of an average.

The entire array.
Can you show us your whole code?
[code]
#include <iostream>


using namespace std;
Last edited on
Okay, so I am going to make some assumptions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int aArray[defaultSize]; //needs to be initialized with a default value
for (int i = 0; i < defaultSize; ++i){
//aArray[i] is set to a default here
}
//assuming you set the array contents starting at position 0
//you only need to check the 0 index

if(aArray[0] == defaultValue){
cout << "ERROR"  << endl;
}
else{
//assuming i is the number of integers input
for (int j = 0; j < i; j++)
                    {
                    sum = sum + aArray[j];
                    }
     avg = (sum/i); //this calc only needs to be done once and can be put out of loop like this
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 'A':
case 'a':
sum = 0;
avg = 0;
if(aArray[i]!=NULL)
     {
         cout << "Error" << endl;
         break;
       }
            else
            {
                for (int j = 0; j < i; j++)
                    {
                    sum = sum + aArray[j];
                    avg = (sum/i);
                    }

cout << endl;
cout <<"\n The average of array is: " << avg << endl;
cout << endl;
break;
            }


It should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 'A':
case 'a':
sum = 0;
avg = 0;
if(i == 0)
     {
         cout << "ERROR!!!!!!!!!!!!!!!" << endl;
         break;
       }
            else
            {
                for (int j = 0; j < i; j++)
                    {
                    sum = sum + aArray[j];
                    avg = (sum/i);
                    }

cout << endl;
cout <<"\n The average of array is: " << avg << endl;
cout << endl;
break;
            }
Last edited on
Thanks man!

Now I'm gonna finish the next two cases.
I am now stuck on case f.

• Input: The letter F.
Output: The output is the most FREQUENT of all the input numbers read so far--in other words, the number that has been read most often (also called the "mode"). If there are several numbers that are equally often, then the smallest number is printed. For example, if there have been four input numbers 2, 2, 6 and 5, then the most frequent is 2. If there has not yet been any input numbers, then the program should print the word "ERROR" instead.
1
2
3
4
5
6
7
8
9
10
11
12
		case 'F':
		case 'f':
        int maxValue = 0;
        for (int j = 0; j < i; j++)
            if(aArray[j] > maxValue)
        {
            maxValue = aArray[j];
        }
            cout << endl;
			cout <<"\n The most frequent value of array is: " << maxValue << endl;
			cout << endl;
			break;


Should be :
1
2
3
4
5
6
7
8
9
case 'F':
	case 'f':
        int mode = aArray[0];
        for(j = 1; j < i; j++)
        if(std::count(&aArray[0], &aArray[i], aArray[j]) >  std::count(&aArray[0], &aArray[i], mode)) mode = aArray[j];

	cout <<"\n The most frequent value of array is: " << mode << endl;
	cout << endl;
	break;


Be sure to include the header <algorithm>
Last edited on
The compiler get an error on line 4.

||=== Build: Debug in Assign 7-A (compiler: GNU GCC Compiler) ===|
C:\Users\cbooa1\Desktop\Assign 7-A\main.cpp||In function 'int main()':|
C:\Users\cboo1\Desktop\Assign 7-A\main.cpp|95|error: name lookup of 'j' changed for ISO 'for' scoping [-fpermissive]|
C:\Users\cbaoo1\Desktop\Assign 7-A\main.cpp|95|note: (if you use '-fpermissive' G++ will accept your code)|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The try this instead.
1
2
3
4
5
6
7
8
9
case 'F':
	case 'f':
        int mode = aArray[0];
        for(int j = 1; j < i; j++)
        if(std::count(&aArray[0], &aArray[i], aArray[j]) >  std::count(&aArray[0], &aArray[i], mode)) mode = aArray[j];

	cout <<"\n The most frequent value of array is: " << mode << endl;
	cout << endl;
	break;
Last edited on
Still the same error, is it because you put:

for(j = 1; j < i; j++)

instead of:

for(int j = 1; j < i; j++)
Yes, it should be for(int j = 1; j < i; j++)
Okay I did that and it worked.
[code]#include <iostream>
#include <algorithm>


using namespace std;

Last edited on
||=== Build: Debug in Assign 7-A (compiler: GNU GCC Compiler) ===|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp||In function 'int main()':|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|101|error: jump to case label [-fpermissive]|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|95|error: crosses initialization of 'int mode'|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|102|error: jump to case label [-fpermissive]|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|95|error: crosses initialization of 'int mode'|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|103|error: 'j' was not declared in this scope|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|106|error: invalid types 'int [99][double]' for array subscript|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|111|error: jump to case label [-fpermissive]|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|95|error: crosses initialization of 'int mode'|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|112|error: jump to case label [-fpermissive]|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|95|error: crosses initialization of 'int mode'|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|115|error: jump to case label [-fpermissive]|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|95|error: crosses initialization of 'int mode'|
C:\Users\KDIadmin\Desktop\Assign 7-A\main.cpp|13|warning: unused variable 'p' [-Wunused-variable]|
||=== Build failed: 12 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


Keep getting the following errors.
1
2
3
4
5
6
7
	int aArray[99];
	double sum;
	double avg;
	double maxValue;
	double n,p;

	int i = 0;

It should be :
1
2
3
4
5
6
7
8
	int aArray[99];
	double sum;
	double avg;
	double maxValue;
	double n,p;

	int i = 0;
	int j;


And change all int j(s) in the switch-statement to j.
So I am unable to test case H
So I am unable to test case H

What are the details for test case 'H'?
Last edited on
Pages: 12