C++ goals: using loops Problem: A local company, Molding Supreme, uses produces
ID: 3803363 • Letter: C
Question
C++ goals: using loops Problem: A local company, Molding Supreme, uses produces molds to cast various shapes. However, the molds become scratched after many uses and need to be replaced which costs a lot of money. Therefore the company has been researching various formulations mold materials and heat nts to prod uce a material that is not too expensive and that resists scratching You are asked to develop a program that will help the researchers analyze the data The researchers use multiple formulations and apply various heat treatments to these formulations. The researchers need a program to determine the highest and second highest hardness for each formulation Your program should ask the user to enter the formulation name. Next your program should ask the user how many heat treatments were used for that formulation. The number of heat treatment ts must be at least 3. The user should then be prompted to enter a hardness value for each of the treatments of that formulation and output the highest and second highest hardness for that formulation. Your code should use a single string object to store the name of the formulation, a while loop to check that the number of heat treatments is at least 3, a for loop to enter the hardness for each heat treatment and determine the highest hardness and second highest hardness values, a while loop to ensure that the hardness is greater than 0 and less than or equal to 11 and a do-while loop to repeat for additional formulations. Sample outputs are given below. Make sure that your code follows the assignment guidelines posted on Canvas. Your code should not use arrays or concepts beyond Chapter 5 of your textbook. Upload your source code as a submission for Project 2. Output 1 what is the name of the formulation? Tungsten Carbide 451 How many heat treatments were used for formulation Tungsten Carbide 451? 5 Enter the hardness for treatment 1 of formulation Tungsten Carbide 451. Enter the hardness for treatment 2 of formulation Tungsten Carbide 45l 8.34 Enter the hardness for treatment 3 formulation Carbide 451. 9.93 the for of Tungsten Enter the hardness for treatment 5 of formulation Tungsten Carbide 451 10.02 The highest hardness for Tungsten Carbide 451 was 10.02 "ohs and the second highest hardness was 9.93 mohs Are there more formulations? (y/n) nExplanation / Answer
code
===========
#include <iostream>
#include <string>
using namespace std;
int main() {
char ch;
do{
string formulation;
float hardness,highest=0,second=0;
int count,highest_i=0,second_i=0;
cout<<" What is the name of the formulation? ";
getline(cin,formulation);
cout<<" How many heat treatments were used for formulation "<<formulation<<"? ";
cin>>count;
for(int i=0;i<count;i++){
cout<<" Enter the hardness for treatment "<<i+1<<" of formulation "<<formulation<<". ";
cin>>hardness;
if (hardness <= 0 || hardness > 11){
cout<<" The hardness must be greater than zero and less than or equal to 11! ";
i--;
}
else{
if (hardness > highest){
second = highest;
second_i = highest_i;
highest = hardness;
highest_i = i;
}
else if (hardness > second){
second = hardness;
second_i = i;
}
}
}
cout<<" Treatment "<<highest_i+1<<" produced the highest hardness for "<<formulation<<" of "<<highest<<" mohs";
cout<<" and treatment "<<second_i+1<<" produced the second highest hardness of "<<second<<" mohs.";
cout<<" Are there more formulations? (y/n)";
}while(ch=='y');
return 0;
}
==============
output
==============
What is the name of the formulation?
How many heat treatments were used for formulation Tungsten Carbide 451? 5
Enter the hardness for treatment 1 of formulation Tungsten Carbide 451. 9.89
Enter the hardness for treatment 2 of formulation Tungsten Carbide 451. 8.34
Enter the hardness for treatment 3 of formulation Tungsten Carbide 451. 9.93
Enter the hardness for treatment 4 of formulation Tungsten Carbide 451. 8.99
Enter the hardness for treatment 5 of formulation Tungsten Carbide 451. 10.02
Treatment 5 produced the highest hardness for Tungsten Carbide 451 of 10.02 mohs and treatment 3 produced the second highest hardness of 9.93 mohs.
Are there more formulations? (y/n) n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.