I'm trying to write a program that takes in grades of an exam.(once the user is done inputting the grades he should press -1. the program should find the max and min grade and compute the average of all the grades.
My problem is that -1 is always the minumum grade computed. i tried fixing this problem by putting intmain1=999 (a very large number), but it also didnt work.
any suggestions?
thank you so much!
#include <iostream>
using namespace std;
int main(){
int grade1,average1;
int max1=0;
int min1=0;
int counter=0;
int sum=0;
cout<<"Enter grades of quiz 1. Use '-1' when there are no more grades."<<endl;
cin>>grade1;
while (grade1!=-1){
cin>>grade1;
counter++;
sum=sum+grade1;
if (grade1>max1)
max1=grade1;
else if (grade1<min1)
min1=grade1;
}
average1=sum/counter;
cout<<"The maximum grade of quiz 1 is"<<max1<<endl;
cout<<"The minumum grade of quiz 1 is"<<min1<<endl;
cout<<"THe average of quiz one is"<<average1<<endl;
}
All you need to do is move the cin>>grade1; from the first line of your while loop to the last. As your code is written, once they enter -1 it does all the logic in the loop and then finally stops, where you want it to stop before processing the -1.
Related
I have been trying to write a simple code in C++ to calculate CGPA. It is a practice code. There is no error in code when I start to build. But it is not running.
I am using codeblocks. I have checked everything. but can not find any problems in it.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g,h,i,a=0, grade[g],hour[h];
char course[10];
float z=0,sum =0, result=0, totalhour=0;
cout<<"How Many Course's do you want to calculate for CGPA: ";
cin>> a;
cout<<endl;
cout<<"Please Enter your Course name, Credit hour & Grade point of the your course"<<endl;
for(i=1;i<=a;i++)
{
cout<<i<<". no Course name: ";
cin>>course[i];
cout<<"-----";
cout<<"Credit Hour: ";
cin>>hour[h];
cout<<"-----";
cout<<"Grade point: ";
cin>>grade[g];
cout<<"\n";
}
for (i=1; i<=a;i++)
{
cout<<i<<".no Course----";
z= grade[g]*hour[h];
cout<<"Grade point X Credit Hour = "<<grade[g]<<" X "<<hour[h]<<" = "<<z<<endl;
sum = sum+z;
totalhour= totalhour+hour[h];
}
result = sum / totalhour;
cout<<"Your total Credit hour Completed is : "<<totalhour<<endl;
cout<<"----Your Total CGPA is -------- = "<<result<<endl;
getch();
return 0;
}
You have some problems in your code:
What happens if user input is larger then 10? you assume it will be smaller (you will get undefined behavior since the array size is 10, and your for loop runs until user input)
int g,h,i,a=0, grade[g],hour[h]; - what is the size of grade and hour? you can not "dynamically" give them a size. You must tell what size they are when you declare them, unless you use dynamic containers such as std::vector.
This is a program to find whether the entered number is a prime number or not.
There is no errors but i always get the the answer as not a prime number for any number.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,count = 0,i;
cout<<"enter the number n";
cin>>n;
for(i=1;i<=n;i++){
if(n%i==0)
count++;
}
if(count==2){
cout<<"It is a prime number";}
else {
cout<<"It is not a prime number";}
getch();
}
When the compilation of a program fails, the compiler usually presents you with information about what the problem in your code is. You should use this information to track down the issue or at least let those you ask for advice know.
Let's go through the compilation errors step by step:
#include<iostream.h>
use #include <iostream> instead
void main()
main should return int. So change your main entry point to: int main(int argc, char* argv[]) and add a return 0; at the very end of your main function.
‘cout’ was not declared in this scope and error: ‘cin’ was not declared in this scope
cout and cin are functions defined in the iostream header you're including. However, these are defined in the std namespace so they are not available in the global scope. So either use std::cout and std::cin or put a using namespace std; before your main function.
With these changes, the code compiles fine and seems to produce the correct output.
#Neeraj Karthikeyan your for loop and if condition are not correct, that is why you are getting not a prime number every time, try to understand below code
#include <iostream>
#include<conio.h>
void main()
{
int n, i, flag=0;
clrscr();
cout << "Enter a positive integer: ";
cin >> n;
for(i=2;i<=n/2;++i) //start i with 2 because if you start with 1 so all number will divisible by 1.
{
if(n%i==0)//if this is true it means number is not prime number
{
flag=1; //here we assign 1 in flag and breaking this block.
break;
}
}
if (flag==0) // if n did not divisible by any number that means flag is still 0
cout << "This is a prime number";
else //this means flag!=0 that means it divided by any number above therefore it is not a prime number
cout << "This is not a prime number";
}
Re the original code: I don't recall the operator precedence offhand, but possibly it is evaluating your loop condition as 'n % (i == 0)'. Try parenthesizing, i.e '(n % i) == 0'. Otherwise the code looks correct to me.
#Bunker Boy: The given algorithm attempts to count how many divisors n has, -including 1 and n-. If exactly 2, prime. Your approach classifies 1 as prime, but nowadays mathematicians generally classify 1 as neither prime nor composite, so technically you are incorrect in that one case.
I've been recycling an assignment to further practice and develop my programming skills in my class and I'm having an issue with 3 DO WHILE loops within another Do While loop.
I'm trying to deny Test scores for test1, 2 and 3 that are less than 1 and greater than 100.
I'm encountering that the loops are not processing what I am inputting for Test1/2/3. It's allowing values out of the while range to pass through. Is there anyone who can suggest or see what I might be doing wrong? Thanks ahead of time guys!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double computeavg (int a, int b, int c);
char lettergrade (double z);
int main ()
{
double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1=0, tottest2=0, tottest3=0, avg1, avg2, avg3; //tottest# - sum of test grades collected for that number test, avg# average for first second or third test
int student=0, avgvar; //average variable, Student number
char grade, ans; // Holds a letter grade, holds a response to a question
do{
student=student+1;
cout<<"Hello Student #"<<student<<endl;
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1>=1||test1<=100);
do{
cout<<"Please input test 2 Grade ";
cin>> test2;
}
while(test2>=1||test2<=100);
do{
cout<<"Please input test 3 Grade ";
cin>> test3;
}
while(test3>=1||test3<=100);
average=computeavg (test1, test2, test3);
cout<<setprecision(0)<<fixed;
cout<<"Your Average is: "<<average<<endl;
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
grade = lettergrade(average);
cout << "Your grade is " << grade << endl;
cout<<"Do you want to grade another student? (y/n)";
cin>>ans;
cout<<"\n";
} while(ans=='y');
I think you need to change do while as below:
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1<1||test1>100);
Your conditions are backward. Look carefully: while(test3>=1||test3<=100). All numbers satisfy at least a portion of the condition. For example, −4 is not greater than or equal to 1, but it is less than or equal to 100, and since your conditions are joined with the || operator, which is the or operator, the condition evaluates to true.
You need to change the condition. This is homework, so I won't tell you precisely what condition you should use instead.
You can use a debugger to discover this problem yourself. You can also discover it by carefully walking through your program by hand. Get a pencil and some paper, and write down the current values of your variables. Keep track of which instruction you're on, and then work your way through the program. When a variable changes, cross out its old value and write down the new one. Eventually, you'll be able to do that in your head, but while you're learning, it helps to just write it down.
Good evening everyone, i am attempting to write code that will determine when a number is largest and smallest.. I have asked some tutors I know for help and they have been stumped on this as well. I can not use functions or arrays or breaks. :/ Which I understand makes the process more difficult.. My professor has stated
"The only decisions staments allowed inside the loop are to determine the largest and smallest value. This means you are not allowed to use a decision to determine if the first number was entered. This is going to require you to prime your loop. We cover priming the loop in the next section but for this assignment it means get the first number before the loop begins. We will also assume that at least one number is going to be input."
I don't understand how he expects us to do something we have not learned yet, but regardless.. This is how far I have gotten on the assignment..
We have to have the user input a value to determine how many values will be input...
I keep receiving an error message after I input how many values I would like to check,
"the variable "num" is being used without being initialized.." But num is in the int!!!
Then have the software basically identify the largest and smallest... Hopefully this makes sense to someone.. If you have any questions, or if I need to clarify anything please let me know, I will do so to the best of my ability..
#include <iostream>
using namespace std;
int main ()
{
int number;
int max=0;
int num;
int min=0;
{ cout << "How many numbers do you want to enter" ;
cin >> number;
for (int i=0; num!=number; i++)
{
cout<<"Enter a num "; /* This is where they are supposed to place in a number, press enter, another number, press enter, until their enter presses = number*/
cin>>num;
if (num>max)
max=num;
if (num<min)
min=num;
}
cout<<"largest number is: "<<max << endl;
cout<<"smallest number is: "<<min << endl;
}
}
This:
for (int i=0; num!=number; i++)
has undefined behavior, num doesn't have a value when this is first evaluated. You meant i != number (or, even better, i < number).
It would be better to use some other way of stopping, i.e. stop when a non-number is entered for instance.
Update: Just to clarify: there are other issues as well, such as min not being initialized in a way that make as many numbers as possible smaller than it. I would probably have gone for min = INT_MAX; or something like that. See <climits> for that constant.
int min=0;
you should change that to
int min=std::numeric_limits<int>::max();
Otherwise, the if the number you entered is greater than 0, it will not be assigned to min.
the variable "num" is being used without being initialized..
Give num a value, you have not given it a value before your loop starts. But that loop concept itself is wrong, try this.
for (int i=0; i < number; i++)
There is two problem in your code, first your min must be a great number like maximum integer, and the second is your for loop should loop on i. I commented lines that you need to change :)
#include <iostream>
#include <limits>
using namespace std;
int main ()
{
int number;
int max=0;
int num;
int min=std::numeric_limits<int>::max(); // change min to maximum integer possible in c++
{
cout << "How many numbers do you want to enter" ;
cin >> number;
//for (int i=0; num !=number; i++) change this line
for (int i=0; i<number; i++) //to this line
{
cout<<"Enter a num "; /* This is where they are supposed to place in a number, press enter, another number, press enter, until their enter presses = number*/
cin>>num;
if (num>max)
max=num;
if (num<min)
min=num;
}
cout<<"largest number is: "<<max << endl;
cout<<"smallest number is: "<<min << endl;
}
}
Guys please help me again with my program. I changed the order of codes in it. Please check what is wrong with my codes. It runs but it doesn't perform the task it should do. It should compute the total of the grades inputted by the user and show the corresponding remarks. Unfortunately, it doesn't work :( please help me
#include<iostream>
#include<conio.h>
using namespace std;
void computePG(int& PG);
void Remark(int PG);
int x, y, z, w, p;
int prelimGrade,yourRemark,PG;
int preliminaryGrade;
int main()
{
int pGrade;
cout<<"Programmed by: Katrina G. Gozo, 1ISC";
cout<<endl<<"\nDate: Aug. 23,2013";
cout<<endl<<"\nThis program intends to compute the PG and make the necessary remarks";
cout<<"\n\nPlease enter your score on quiz 1 ";
cin>>x;
cout<<"\nPlease enter your score on quiz 2 ";
cin>>y;
cout<<"\nPlease enter your score on quiz 3 ";
cin>>z;
cout<<"\nPlease enter your score on prelims ";
cin>>p;
computePG(pGrade);
Remark(pGrade);
getch();
}
void computePG(int& PG)
{
PG = x/30 * 20 + y/50 * 20 + z/40 * 20 + w/100 * 40;
cout << "\nYour prelim grade is " << PG;
}
void Remark(int PG)
{
if (PG>=90)
cout<<"A." <<endl;
else if (PG>=80)
cout<<"B."<<endl;
else if (PG>=70)
cout<<"C."<<endl;
else if (PG>=60)
cout<<"D."<<endl;
else
cout<<"E."<<endl;
}
You're most likely running afoul of integer arithmetic. Note: when dividing an integer by another integer, you get an integer result (rounded towards zero).
So you'll want to use double as the type of PG and pGrade, and make the constants in computePG floating points numbers as well, by writing them as 30.0, 20.0, etc.
You should probably use a double for your variable "PG," that way you'll have adequate decimal accuracy.
Also, you may want to avoid global variables in the future, because I'm guessing it's how you made this error - you never assign a value to w before using it, which means it is assigned the value 0 by the compiler and may be what is screwing up your result.