How to use test same condition for multiple variables in C++ - c++11

#include <iostream>
int main()
{
using namespace std;
int P, C, M, marks;
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P;
cout << "Enter Marks in Chem\n";
cin >> C;
cout << "Enter Marks in Maths\n";
cin >> M;
cout << "Grades\n\nPhysics Chemistry Maths\n";
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80)
cout << "B+";
else if (marks <= 90)
cout << "A";
else if (marks <= 95)
cout << "A+";
else
cout << "Error";
return 0;
}
Is there any way to run above if statement for multiple values of marks like
marks = P
then test for marks = C, marks = M and give output respectively,

You could make your program more modular by implementing the test marks logic in a separate function and name it something like checkGrade and pass as an argument the values you took as input.
For example:
#include <iostream>
using namespace std; //I moved this command outside the main function so it applies globally
void checkGrade(int);
int main()
{
int P, C, M;
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P;
cout << "Enter Marks in Chem\n";
cin >> C;
cout << "Enter Marks in Maths\n";
cin >> M;
cout << "Grades\n\nPhysics Chemistry Maths\n";
checkGrade(P);
checkGrade(C);
checkGrade(M);
return 0;
}
void checkGrade(int marks){
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80)
cout << "B+";
else if (marks <= 90)
cout << "A";
else if (marks <= 95)
cout << "A+";
else{cout << "Error";}
}
Or you could use an array like MadaZZ suggested to eliminate the need to call checkGrade() separately for every user input.

Make an array of marks and a function to check respective grades.
#include <iostream>
using namespace std;
void checkGrade(int marks) //function to check grade
{
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80){
cout << "B+";
}
else if (marks <= 90){
cout << "A";
}
else if (marks <= 95){
cout << "A+";
}
else{
cout << "Error";
}
}
int main()
{
cout << "Enter no. of students\n\n";
int students;
cin >> students;// Take input of no. of students
int P[students], C[students], M[students]; //Declare array of marks, with each index depicting each student. Size of the array would be the number of students you entered
for( int i = 0; i < students; i++ )
{
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P[i];
cout << "Enter Marks in Chem\n";
cin >> C[i];
cout << "Enter Marks in Maths\n";
cin >> M[i];
cout << "Grades\n\nPhysics Chemistry Maths\n";
int count = 3;
while(count) //To check grades in different subjects, the loop runs 'count' number of times ie. for each subject.
{
if(count == 3){
cout<<"\n Grade in Physics ";
checkGrade(P[i]); //Function called to check grade
}
else if(count == 2){
cout<<"\n Grade in Chemistry ";
checkGrade(C[i]); //Function called to check grade
}
else if(count == 1){
cout<<"\n Grade in Maths ";
checkGrade(M[i]); //Function called to check grade
}
count--;
}
}
return 0;
}
The above program stores data for all the students in respective marks array. The checkGrade function takes marks as input and displays the grade one by one. I would suggest you google to learn the concepts and solve some problems.

Related

if first input is larger than 2nd input it will choose 2 instead of 1

I'm having some trouble on my code if yourcurrency is larger than convert the convert will choose the 2 instead of 1
can't figure it out what's wrong with my code i've tried on different compiler I think the issue is the code itself can you help me guys?
#include <iostream>
#include <string>
using namespace std;
int picked[5] = {0,0,0,0,0};
int main(){
string currentvalue[5][5] = {{"1","0.01818484","0.015423716","2.4462308","0.019060361"},{"55.012111","1","0.84815483","134.47933","1.0482778"},{"64.90818","1.1789799","1","158.58285","1.2359668"},{"0.40901121","0.0074303636","0.0063022858","1","0.0077907482"},{"52.511804","0.95391572","0.80907737","128.35738","1"}};
string currency[5] = {"Philippine Peso","euro","pounds","yen","usd"};
int yourcurrency;
int convert;
int timesToRun = 5;
int number = 1;
system("COLOR 0a");
cout << "Choose your currency \n" << endl;
for (int counter = 0 ; counter < timesToRun; counter++)
{
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
cout << "\nOption: ";
cin >> yourcurrency;
system("CLS");
yourcurrency = yourcurrency - 1;
picked[yourcurrency] = 1;
cout << "Select your currency you want to convert into \n" << endl;
number = 1;
for (int counter = 0; counter < timesToRun; counter++)
{
if (picked[counter] != 1){
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
}
cout << "\nOption: ";
cin >> convert;
system("CLS");
cout << currency[yourcurrency]+ " - " + currency[convert];
cout << " [" + currentvalue[yourcurrency][convert] + "] " << endl;
cout << "Amount: ";
int cash;
cin >> cash;
double value = stof(currentvalue[yourcurrency][convert]);
double total = cash * value;
cout << currency[convert]<< ": " << total;
}

Minim of the even elements of the matrix and the maximum of the prime elements

I have to calculateand then display in OOP of C++
Minimum of the even elements of the matrix and the maximum of the prime elements
As i made up my code
My problem is that i have no idea how to make so that the max value from a matrix to be a prime number and I have no clue if I should work with a for( ) and then use a if (value%i==0) or if there can be a better method.
#include <iostream>
using namespace std;
class matrix3
{
int a[10][10], b[10][10], c[10][10], d[10][10], e[10][10], f[10][10], x, y, i, j, l1,l2,max, max2 = 0, min = 0, save, c1, c2;
public:
void values();
void transpose();
void sum();
void diff();
void linie();
void coloana();
void minimul_elementelor_pare();
void maximul_elementelor_prime();
};
void matrix3::values()
{
cout << "Enter the rows "; cin >> x;
cout << "Enter the columns "; cin >> y;
cout << "Enter elements of first matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> a[i][j];
}
}
cout << "Enter elements of second matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> c[i][j];
}
}
}
void matrix3::sum()
{
cout << "Sum of Matrices 1 and 2 is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
e[i][j] = a[i][j] + c[i][j];
cout << e[i][j] << "";
}
cout << endl;
}
}
void matrix3::diff()
{
cout << "Difference of Matrices 1 and 2 (1-2) is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
f[i][j] = a[i][j] - c[i][j];
cout << f[i][j] << "";
}
cout << endl;
}
}
void matrix3::transpose()
{
cout << "transpose of the matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
b[i][j] = a[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
cout << "Transpose of the second matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
d[i][j] = c[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
}
void matrix3::coloana()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n c1: ";cin >> c1;
cout << "\n c2: ";cin >> c2;
for (i = 1; i <= x; i++)
{
save = a[i][c1];
a[i][c1] = a[i][c2];
a[i][c2] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::linie()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n l1: ";cin >> l1;
cout << "\n l2: ";cin >> l2;
for (i = 1; i <= x; i++)
{
save = a[l1][j];
a[l1][j] = a[l2][j];
a[l2][j] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::minimul_elementelor_pare()
{
cout << "Minimul Elementelor Pare";
for (i = 1;i <= x;i++)
{
if (x % 2 == 0)
{
if (a[i][j] < min)
min = a[i][j];
}
min++;
}
cout << " " << min;
}
void matrix3::maximul_elementelor_prime()
{
cout << "max matrice" << endl;
max=a[1][1];
for (i = 1;i <= x/2;i++)
{
for (j = 1;j <= y/2;j++)
if (x%i != 0 && max < a[i][j])
max = a[i][j];
}
cout << " maximul din matrice este " << max;
}
int main()
{
int input;
char ch;
matrix3 m;
m.values();
do
{
cout << "Enter your choice\n";
cout << " 1. Sum of 1 and 2\n" << " 2. Difference of 1 and 2\n" << " 3. Transpose of both 1 amd 2\n" << "4. Linii\n" << "5. Minim\n" << "6.Linie\n"<<"7. Maxim\n";
cin >> input;
switch (input)
{
case 1:
m.sum();
break;
case 2:
m.diff();
break;
case 3:
m.transpose();
break;
case 4:
m.coloana();
break;
case 5:
m.coloana();
break;
case 6:
m.minimul_elementelor_pare();
break;
case 7:
m.maximul_elementelor_prime();
}
cout << "\nDo another y/n?\n";
cin >> ch;
} while (ch != 'n');
cout << "\n";
system("pause");
return 0;
}```

End program after breaking from loop

while (1)
{
cout << "Enter number and unit: ";
cin >> number;
cin >> unit;
if (unit == "in")
{
result = 2.54f * number;
cout << number << " Inches are " << result << " centimeters!" << endl;
}
else if (unit == "ft")
{
result = 0.3f * number;
cout << number << " Feet are " << result << " meters!" << endl;
}
else if (unit == "yd")
{
result = 0.9f * number;
cout << number << " Yards are " << result << " meters!" << endl;
}
else if (unit == "mi")
{
result = 1.6f * number;
cout << number << " Miles are " << result << " kilometers!" << endl;
}
else
{
cout << "\tINVALID!!!\n...the program will now exit...\n";
cin.clear();
cin >> unit;
cin >> number;
break;
}
}
cout << "Thank you for using English to Metric!\n" << endl;
return 0;}
What am I missing in order to get the last cout and exit the program without having to input anything after break?
The break in the else clause will exit the loop, but immediately before it you perform
cin.clear();
cin >> unit;
cin >> number;
break;
The cin >>s are why the program is waiting for additional input.
Without them, the cin.clear() is unnecessary, unless you plan to do additional cin operations later.
else
{
std::cin.clear(); // optional
break;
}
} // end while
std::cout << "Thank you\n";
Will work.

Pairs x,y verify equation

I have to build a program as bellow:
User enters 3 Integers: a, b, c
User enters one integer n, and then n integers (ascending, and different numbers)
Program checks all possible pairs (x,y) (with x!=y) of the numbers entered if verifies the equation ax^2 + by^2 = c and prints all pairs which verifies the equation.
I done the program as bellow:
#include<iostream>
using namespace std;
int main() {
int a,b,c,n,i,j;
cin >> a;
cin >> b;
cin >> c;
cin >> n;
int num[n];
for(i=0;i<n;i++) {
cin >> num[i];
}
for (i=0;i<n;i++)
for (j=i+1;j<n;j++) {
if(a*num[i]*num[i]+b*num[j]*num[j] == c) {
cout << "(" << num[i] << "," << num[j] << ")";
}
if(a*num[j]*num[j]+b*num[i]*num[i] == c) {
cout << "(" << num[j] << "," << num[i] << ")";
}
}
return 0;
}
I made it by O(nlogn) with two 'for' statements but i know it could be done by O(n).
NOTE THAT MY PROGRAM WORKS AND I DON'T NEED TO ADD EXPECTED OUTPUT AND MY CURRENT OUTPUT AS YOU SAID IN THE COMMENTS. I ONLY WANT IT TO BE O(N) not O(nlogn) -> I WANT AN OPTIMIZED VERSION OF THE CODE!
How can I do this?
Example of running program: a=1, b=1, c=20
Then n = 5
Then n numbers: 2 3 4 9 18
Program will show all pairs (x,y) which verifies the equation x^2 + y^2 = 20. In this case it shows (2,4)
and (4,2).
Thank you!
Assuming 0 based index...
Set i=0
Set j=n-1
While i<n or j>=0
Set sum=a(num[i]^2)+b(num[j^2)
If sum==c then found pair, and increase i
If sum<c increase i
If sum>c decrease j
I found exactly this problem solved here: http://lonews.ro/educatie-cultura/22899-rezolvare-admitere-universitatea-bucuresti-2015-pregatire-informatica.html and changed a bit to show the pairs (it originally shows the number of pairs).
#include <iostream>
using namespace std;
int main()
{
int a, b, c, n, i=0;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "n = ";
cin >> n;
int s[n];
for(i=0; i<n; i++) {
cout << "s[" << i+1 << "] = ";
cin >> s[i];
}
int j=n-1;
i = 0;
while(j>=0 || i<n) {
if(a*s[i]*s[i] + b*s[j]*s[j] == c) {
cout << "(" << s[i] << "," << s[j] << ") ";
i++;
}
if(a*s[i]*s[i] + b*s[j]*s[j] < c) {
i++;
}
if(a*s[i]*s[i] + b*s[j]*s[j] > c) {
j--;
}
}
return 0;
}

C++ Numeric values are not printing correctly

Main Program
#include <iostream>
#include <iomanip>
#include "pizza.h"
using namespace std;
int main() {
menuItem item;
menu:
item.getMenu();//Menu Prompt
cout << "\nI would like number: " << flush;
int menuChoice;
cin >> menuChoice;
while (menuChoice < 1 || menuChoice > 3) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
}
if (menuChoice == 1) {
item.getPizza();
}
if (menuChoice == 2) {
item.getSandwhich();
}
if (menuChoice == 3) {
item.getSide();
}
int ready4checkout;
cout << "\n\n~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~" << endl;
cout << "\nAre you ready to checkout?\n" << endl;
cout << "checkout = 1\nAdd to the order = 2" << endl;
cout << "\nI would like to:" << flush;
cin >> ready4checkout;
if (ready4checkout == 1) {
item.addcheckout();
}
if (ready4checkout == 2) {
goto menu;
}
cin.ignore();
cin.get();
}
Header File
#include<iostream>
#include <iomanip>
using namespace std;
class menuItem {
protected:
float smallPizza = 5.99;
float mediumPizza = 6.99;
float largePizza = 9.99;
float halfSandwhich = 6.00;
float wholeSandwhich = 8.00;
float bonelessWings = .70;
float breadStix = 4.99;
public:
float checkout;
float pizzaCheck;
float sandCheck;
float sideCheck;
menuItem item(float smallPizza, float mediumPizza, float largePizza, float halfSandwhich, float wholeSandwhich, float bonelessWings, float breadStix,float checkout);
string getMenu() {
string menu;
cout << "~*~*~*~*~*~*~*~ Welcome to Pizza Italiano! ~*~*~*~*~*~*~*~*~\n\n" << endl;
cout << "~~~~~~~~~~~~~~~~~ Main Menu ~~~~~~~~~~~~~~~~~~" << endl;
cout << "Enter the value of the food that you wish to devour:\n" << endl;
cout << "\n1.Pizza\n2.Sandwhich\n3.Appetizer" << endl;
return menu;
}
string getPizza() {
float pizzaCheck;
string pizza;
float pizzaSize;
pizza:
cout << " \n~~~~~~ How would you like your pizza made? ~~~~~~\n" << endl;
cout << " Size" << endl;
cout << "-----------" << endl;
cout << "1. Large $9.99 \n2. Medium $6.99 \n3. Small $5.99" << endl;
cout << "\nI would like the number: " << flush;
cin >> pizzaSize;
cin.ignore();
while (pizzaSize < 1 || pizzaSize > 3) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto pizza;
}
if(pizzaSize == 1) {
cout << "\n****You have selected a Large pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = largePizza + pizzaCheck;
} else if(pizzaSize == 2) {
cout << "\n****You have selected a Medium Pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = mediumPizza + pizzaCheck;
} else {
cout << "\n****You have selected a Personal pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = smallPizza + pizzaCheck;
}
cin.get();
return pizza;
}
string getSandwhich() {
string sandwhich;
float sandSize;
Sandwhich:
cout << "~~~~~~~~~~~~ What size would you like your sandwhich to be? ~~~~~~~~~~~~~~~\n\n" << endl;
cout << "1. Six inch $6.00\n2. Twelve inch $8.00" << endl;
cout << "I would like number:" << flush;
cin >> sandSize;
while (sandSize < 1 || sandSize > 2) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto Sandwhich;
}
if (sandSize == 1) {
cout << "**** You have selected a six inch sub ****" << endl;
sandCheck = halfSandwhich + sandSize;
} else {
cout << "**** You have selected a 12 inch sub ****" << endl;
sandCheck = wholeSandwhich + sandSize;
}
return sandwhich;
}
string getSide() {
string Appetizer;
float side;
sides:
cout << "~~~~~~~~~~~~~~~ Which appetizer would you like? ~~~~~~~~~~~~~~~~~\n\n" << endl;
cout << "1. Boneless Wings .70\n2. BreadStix $4.99\n\n" << endl;
cout << "I would like the number:" << flush;
cin >> side;
while (side < 1 || side > 2) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto sides;
}
if (side == 1) {
cout << "\n**** You have selected Boneless wings ****\n" << endl;
cout << "How many wings would you like?\n" << endl;
cout << "I would like this many wings:" << flush;
int wings;
cin >> wings;
cout << "\n**** You will recieve " << wings << " wings. ****" << endl;
side = sideCheck;
sideCheck = bonelessWings += sideCheck;
} else {
cout << "\n**** You have selected an order of breadstix ****" << endl;
side = sideCheck;
sideCheck = breadStix += sideCheck;
}
return Appetizer;
}
float addcheckout() {
checkout = pizzaCheck;
cout << "\nYour total is " << checkout << endl;
cout << "\nThank You for your purchase!";
return checkout;
}
};
At the end of the program, when it gives the checkout total, it gives a value something like -1.07374e.+008
I am still learning how to code, and I am stumped on this one. Any help would be much appreciated!
I think you shadow your member field pizzaCheck in your method getPizza, so that you actually don‘t write to the member field but to your local var declared at the first line of that method.
Try removing that local var. (I did not test it yet though)

Resources