1. Please write a C++ program to display the star pattern on the American flag.
ID: 3742794 • Letter: 1
Question
1. Please write a C++ program to display the star pattern on the American flag. In this program, you don't have to define a class, just use the main function and some user defined functions, if you choose to do so. It can be named as stars.cpp
2. Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display the student's name, and a letter grade such as A, B, C, D, or F. It shall have a Student class with necessary private data members and constructor and public methods, such as getName, getGrade, calcScore, convertToGrade, etc. Try to create a Student object in main(), then invoke its methods to perform the tasks. You may choose to put everything in one cpp file, or to separate the declaration and definition of Student class, then you need to submit 3 files: the main app, Student.cpp and Student.h
Explanation / Answer
//The given below cpp code fro print star pattern on the American flag.
#include <iostream>
using namespace std;
int main() {
int i=0,j,k;
while(i<=4)
{
for(j=1;j<=6;j++)
{
cout<<"* ";
}
cout<<" ";
for(k=1;k<=5;k++)
{
cout<<" *";
}
cout<<" ";
i++;
}
return 0;
}
The below given code is for the Student details.
{
cout<<"Enter your register number"<<" ";
cin>>rno;
cout<<"Enter your name"<<" ";
cin>>name;
}
//function to read marks obtained.
void student::getGrade()
{
int i;
for(i=1;i<=10;i++)
{
cout<<"Enter mark for subject"<<i<<": ";
cin>>mark[i-1];
if(mark[i-1]<=100)
tot = tot+mark[i-1];
}
}
//function to calculate score.
void student::calcScore()
{
avg=tot/10;
for(i=1;i<=10;i++)
{
if(mark[i-1]>=40)
strcpy(result,"pass");
else
{
strcpy(result,"fail");
break;
}
}
if(strcmp(result,"pass")==0)
{
if(avg>=90)
strcpy(grade,"A grade");
else if(avg<90&&avg>=80)
strcpy(grade,"B grade");
else if(avg<80&&avg>=70)
strcpy(grade,"C grade");
else if(avg<70&&avg>=60)
strcpy(grade,"D grade");
}
else
strcpy(grade,"F grade");
}
//function to display the result.
void student::convertToGrade()
{
cout<<" Register Number: "<<rno<<" ";
cout<<"Name: "<<name<<" ";
cout<<"Total Marks obtained "<<tot<<" ";
cout<<"Avarage: "<<avg<<" ";
cout<<"Grade: "<<grade;
}
int main()
{
cout<<" RESULT ";
student s;// class object
s.getName();
s.getGrade();
s.calcScore();
s.convertToGrade();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.