Greetings, I am having problems with the following question. When i do it, it se
ID: 3666381 • Letter: G
Question
Greetings,
I am having problems with the following question. When i do it, it seems to go in infinity.
Problem 1. Use a nested loop to print out the following:
********
*******
******
*****
****
***
**
*
I'm also having issues with the following problem.
Problem 2. Comprehensive programming: (file I/O, if…else, for loop, output manipulators)
Cheap Chirps is a pet store specializing in pet birds. The store needs a new inventory program and has hired you to develop the program. Write a C++ program that will:
1.Read an inventory list of birds from a data file called BIRD.txt. The file contains information on bird species in the store. There are four lines in the file. Each line of the file has type of bird (no larger than 20 characters), price of the bird, and the consumed food in pounds monthly. Use for loop to read the related information.
BIRD.txt
MACAW 2,500.00 20
AMAZON 550.75 15
COCKATIEL 123.50 2
PARAKEET 45.50 1
2.Your program will find if the bird is a large hookbill or a small hookbill (Large hookbills eat 15 or more pounds of food a month.) Use a character 'L' to represent a large hookbill and an 'S' to represent a small hookbill.
3.Your program will also calculate the average price of a bird and the average amount of food eaten in one month.
4.Print the output so that it is organized as below. Note the values for the averages are printed to two decimal places. All printing is done in the main function.
BIRD PRICE FOOD HOOKBILL
----------------------------------------------------------------------------
MACAW 2500.00 20 L
AMAZON 550.75 15 L
COCKATIEL 123.50 2 S
PARAKEET 45.50 1 S
The average price is: 804.94
The average food is: 9.50
Explanation / Answer
program 1:
/**C++ program that prints a patters of stars*/
#include<iostream>
using namespace std;
int main()
{
//set a character star
char star='*';
//set COLUMNS=8
int COLUMNS=8;
//set size
int size=COLUMNS;
//run outer loop for 8 times
for(int index=1;index<=COLUMNS;index++)
{
for(int index=1;index<=size;index++)
cout<<star;
cout<<endl;
//decrement the size of inner loop by one
size=size-1;
}
system("pause");
return 0;
}
sample output:
********
*******
******
*****
****
***
**
*
------------------------------------------------------------------------------------------------------------------------------
Program 2:
/**C++ program that reads a text file BIRD.txt which of
four lines . Each line contains name, price and food.
The program reads the text and prints the files and finds
the HOOKBILL and avaerage price and averae food */
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//create a strucure Bird
struct Bird
{
//name of size 20 characters long
char name[20];
//declare price
double price;
//declare food
int food;
};
int main()
{
const int NUM_LINES=4;
struct Bird bird[NUM_LINES];
ifstream fin;
fin.open("BIRD.txt");
if(!fin)
{
cout<<"File doesnot exist"<<endl;
return 1;
}
//declare two double type variables and set to zero
double totalPrice=0;
double totalFood=0;
for(int index=0;index<NUM_LINES;index++)
{
//read name , price and food
fin>>bird[index].name>>bird[index].price>>bird[index].food;
//Add price to total price
totalPrice+=bird[index].price;
//Add price to totalFood
totalFood+=bird[index].food;
}
cout<<setw(10)<<"BIRD"<<setw(10)<<"PRICE"<<setw(10)<<"FOOD"<<setw(10)<<"HOOKBILL"<<endl;
cout<<"-------------------------------------"<<endl;
for(int index=0;index<NUM_LINES;index++)
{
//read name , price and food
cout<<setw(10)<<bird[index].name<<setw(10)<<bird[index].price<<setw(10)<<bird[index].food;
//Set HOOKBILL as L if food is greater than 15
if(bird[index].food>=15)
cout<<setw(10)<<"L"<<endl;
else
//otherwise set HOOKBILL to S
cout<<setw(10)<<"S"<<endl;
}
//print average price
cout<<"The average price is "<<setprecision(5)<<totalPrice/4.0<<endl;
//prit average food
cout<<"The average food is "<<setprecision(5)<<totalFood/4.0<<endl;
system("pause");
return 0;
}
Sample output:
BIRD PRICE FOOD HOOKBILL
---------------------------------------------------------
MACAW 2500 20 L
AMAZON 550.75 15 L
COCKATIEL 123.5 2 S
PARAKEET 45.5 1 S
The average price is 804.94
The average food is 9.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.