// A high school is holding a recycling competition // This program allows a use
ID: 3564664 • Letter: #
Question
// A high school is holding a recycling competition
// This program allows a user to enter a student's
// year in school (1 through 4) and number of cans collected
// Data is entered continuously until the user enters 9
// After headings, output is four lines --
// one for each school year class
start
Declarations
num year
num cans
num SIZE = 4
num QUIT = 9
num collectedArray[SIZE] = 0, 0, 0, 0
string HEAD1 = "Can Recycling Report"
string HEAD2 = "Year Cans Collected"
output "Enter year of student or ", QUIT, " to quit "
input year
while year <> QUIT
output "Enter number of cans collected "
input cans
collectedArray[year - 1] = collectedArray[year - 1] + cans
output "Enter year of student or ", QUIT, " to quit "
input year
endwhile
output HEAD1
output HEAD2
year = 1
while year <= SIZE
output year, collectedArray[year - 1]
year = year + 1
endwhile
stop
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int year,cans;
int SIZE = 4;
int QUIT = 9;
int collectedArray[4] = {0,0,0,0};
string head1 = "Can Recycling Report";
string head2 = "Year Cans Collected";
cout<<"Enter year of student or 9 to quit ";
cin>>year;
while(year!=QUIT){
cout<<"Enter number of cans collected ";
cin>>cans;
collectedArray[year-1] = collectedArray[year-1] + cans;
cout<<"Enter year of student or "<<QUIT<<" to quit ";
cin>>year;
}
year=1;
while(year<=SIZE){
cout<<year<<" "<<collectedArray[year-1]<<" ";
year++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.