Struggling with an infinite loop and a sub for global variables

I have 2 questions.

My first program I am struggling with an infinite loop but I dont understand why,

The second I dont know how to maintain the value of "workers" without using global variables

Program 1 instructions:
Create an array and initialize it wiht values: 5,15,25,30 (in one statement)
Create another array and initialize it with values: 2,15,20,30 (in one statement)
Compare the two arrays and print out "equal" or "not equal" for the elements that differ.
All logic should be in a single function- main()

Program 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Name
//Lab 7A
#include <iostream>
using namespace std;
int main()
{
    //create the arrays
	const int SIZE = 4;
	int firstArray[SIZE] = {5,15,25,30};
	int secondArray[SIZE] = {2,15,20,30};
	int count = 0;
	

    if (count < size)
	{
	    if (firstArray[count] == secondArray[count])
	    {
	        cout << firstArray[count] << " and " << secondArray[count] << " are equal.\n";
	        count ++;
	    }
	    else
	    {
	        cout << firstArray[count] << " and " << secondArray[count]<< " are not equal.\n";
	        count++;
	    }
	}
	
	
}


Instructions for Program 2:

Write a program that from main will prompt an employer for the ages of 5 of their workers and store this information in an array.
Call a function that will average the ages in the input array and print the result to the screen.

DO NOT USE GLOBAL VARIABLES

Program 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Name
//Lab 7C

#include <iostream>
using namespace std;
void average()

int main()
{
	int workers, age;
	
	cout << "Hello! How many ages would you like to input today?" << endl;
	cin >> workers;
	
    int employees[workers];
    
    for (age = 0; age <= workers - 1; age++)
    {
        cout << "Please enter the age for worker # " << age + 1 << ": " << endl;
        cin >> employees[age];
    }
    
    average();
}

void average()
{
    int runningTotal = 0;
    int count;
    for (count = 0; count < workers; count++)
    {
        runningTotal += employees[count];
    }
    
    cout << "The average age for the " << workers << " employees you entered is:" << runningTotal/workers;
}
Last edited on
My first program I am struggling with an infinite loop but I dont understand why,

Your first program doesn't even have a loop. If you change size to SIZE it will compare the first elements. If you change if to while it will compare all elements.

The second I dont know how to maintain the value of "workers" without using global variables

Pass the array and the number of elements in the array as arguments to the function.
Last edited on
prog1:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <algorithm>

int main() {
	constexpr size_t SIZE {4};

	const int firstArray[SIZE] {5,15,25,30};
	const int secondArray[SIZE] {2,15,20,30};

	std::cout << (std::equal(firstArray, firstArray + SIZE, secondArray) ? "Equal\n" : "Not Equal\n");
}


Or without using std::equal():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
	constexpr size_t SIZE {4};

	const int firstArray[SIZE] {5,15,25,30};
	const int secondArray[SIZE] {2,15,20,30};

	bool same {true};

	for (size_t i {}; same && i < SIZE; ++i)
		same = firstArray[i] == secondArray[i];

	std::cout << (same ? "Equal\n" : "Not Equal\n");
}

Last edited on
@seeplus, what is your motivation for making that post?
prog2:

L15 is not standard C++ as the size of the array needs to be known at compile time.

Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void average(const unsigned employees[]);

constexpr size_t NoEmp {5};

int main() {
	unsigned employees[NoEmp] {};

	for (size_t e {}; e < NoEmp; ++e) {
		std::cout << "Please enter the age for worker # " << e + 1 << ": ";
		std::cin >> employees[e];
	}

	average(employees);
}

void average(const unsigned employees[]) {
	double runningTotal {};

	for (size_t e {}; e < NoEmp; ++e)
		runningTotal += employees[e];

	std::cout << "The average age for the " << NoEmp << " employees you entered is: " << runningTotal / NoEmp << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main()
{
    //create the arrays
    const int SIZE = 4;
    int firstArray[SIZE] = {5,15,25,30};
    int secondArray[SIZE] = {2,15,20,30};
    int count = 0;
    
    
    while (count < SIZE) // <-- if only checks the first instance, while keeps going
    {
        if (firstArray[count] == secondArray[count])
        {
            cout << firstArray[count] << " and " << secondArray[count] << " are equal.\n";
        }
        else
        {
            cout << firstArray[count] << " and " << secondArray[count]<< " are not equal.\n";
        }
        
        count++;
    }
}



5 and 2 are not equal.
15 and 15 are equal.
25 and 20 are not equal.
30 and 30 are equal.
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

void average(int[], int);

int main()
{
    const int NO_WORKERS = 5;
    int age[NO_WORKERS];
    
    for (int i = 0; i < NO_WORKERS; i++)
    {
        cout << "Please enter the age for worker # " << i + 1 << ": ";
        cin >> age[i];
    }
    
    average(age, NO_WORKERS);
}

void average(int A[], int N)
{
    double runningTotal = 0;
    
    for (int i = 0; i < N; i++)
    {
        runningTotal += A[i];
    }
    
    cout
    << "The average age for the " << N << " employees you entered is: "
    << runningTotal/N << '\n';
}


Please enter the age for worker # 1: 23
Please enter the age for worker # 2: 42
Please enter the age for worker # 3: 34
Please enter the age for worker # 4: 17
Please enter the age for worker # 5: 67
The average age for the 5 employees you entered is: 36.6
Program ended with exit code: 0
Topic archived. No new replies allowed.