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;
}
|