What's new

Closed C++ problem

Status
Not open for further replies.

Kingrhejj

Addict
Joined
Oct 14, 2017
Posts
191
Reaction
34
Points
105
Need help about c++ mga ka phc.

ayaw mag stop ng program
Sa expression na
while ( n > 0) ;
Example.) n = -90
90 < 0; ( false )

Given problem:
Write a program written in c++ language to calculate the average of an unknown number of grade .

*Prompts the user to enter a grade each time through the loop.

* update the sum and number of grade.

*prompts the user to enter a negative grade after the last valid grade.

*continue looping while the input grade is not negative.

Kaso ayaw mag stop ng program kahit mag enter ako ng negative grade .



#include<iostream>
using namespace std;
int main () {
int i, n, b, t, sum = 0, num[100];
float ave;
for ( i = 0; ; i ++) {
cout<<i+1<<".Enter a Grade : ";
cin >> num;
t++;
sum += num;
ave = sum /t;
b=ave;
cout << " Sum : " << sum << endl;
cout << " ENTER NEGATIVE GRADE : ";
cin >> num;
}while ( n > 0 );
b=ave;
cout << " Average : " << b<< " % " <<endl;

return 0;
}
 
hindi pwede magsama ang while saka for sa isang block.
For for loop lang.
While while loop lang.
 
Hindi kailangang array yung num mo.
Hindi mo na kailangan yung variable b and n mo.
Basta hindi maganda flow ng code mo.
 
Paki enlighten ako sa part na to
*prompts the user to enter a negative grade after the last valid grade. // MAGLAGAY NG NEGATIVE GRADE PAGKATAPOS NG VALID GRADE? ANO UN ? pano mo masasabing valid grade? Positive?

*continue looping while the input grade is not negative. // ANO RAW? magloloop kapag hindi negative? e sabi sa taas maglagay ng negative grade after valid?

HAHA GULO
 
Paki enlighten ako sa part na to
*prompts the user to enter a negative grade after the last valid grade. // MAGLAGAY NG NEGATIVE GRADE PAGKATAPOS NG VALID GRADE? ANO UN ? pano mo masasabing valid grade? Positive?

*continue looping while the input grade is not negative. // ANO RAW? magloloop kapag hindi negative? e sabi sa taas maglagay ng negative grade after valid?

HAHA GULO
Ibig sabihin yata non mag cocontinue ang program sa pag henge ng grade pag false ang hinihinging negative grade
 
Unknown grades po kc,,, pwdeng isa,dalawa or so on,, kaya humihingi ng negative grade para mag stop ang program,,
Kylangan pa kcng gamitan ng break; para mastop ang program
 
Ganito lang format ng loop.

while (parameter)
{
code
}

do
{
code
} while(bool)

for(statements)
{
code
}

walang
for(statemetns)
{
code
} while(bool)

gets mo na?
 
Ito na pag aralan mo, compile mo para makita mo.
Code:
#include <iostream>
using namespace std;

// Global Dec
int programEnd = 0 , count = 0;

Func1(){
    // Declarations
    int grade, sum;
    float average;
   
    cout << "Enter a grade: ";
    cin >> grade;
    if (grade < 0){
        programEnd = 1;
    }
   
    // Computation
    sum += grade;
    count += 1;
    average = sum / count;
   
    cout << "Current grade total is: " << sum << endl;
    cout << "Current number of grades: " << count << endl << endl;
    cout << "Current Average is: " << average << endl << endl << endl;
}

main(){
    cout << "The Program will end if you input a NEGATIVE GRADE" << endl << endl;
   
    for(programEnd; programEnd < 1;){
        Func1();
    }       
}
 
Ito na pag aralan mo, compile mo para makita mo.
Code:
#include <iostream>
using namespace std;

// Global Dec
int programEnd = 0 , count = 0;

Func1(){
    // Declarations
    int grade, sum;
    float average;
  
    cout << "Enter a grade: ";
    cin >> grade;
    if (grade < 0){
        programEnd = 1;
    }
  
    // Computation
    sum += grade;
    count += 1;
    average = sum / count;
  
    cout << "Current grade total is: " << sum << endl;
    cout << "Current number of grades: " << count << endl << endl;
    cout << "Current Average is: " << average << endl << endl << endl;
}

main(){
    cout << "The Program will end if you input a NEGATIVE GRADE" << endl << endl;
  
    for(programEnd; programEnd < 1;){
        Func1();
    }      
}
Gawa mo ba yn boss or copy paste mo lng ?
May error kc. Btw ty :)
 
Ito na pag aralan mo, compile mo para makita mo.
Code:
#include <iostream>
using namespace std;

// Global Dec
int programEnd = 0 , count = 0;

Func1(){
    // Declarations
    int grade, sum;
    float average;
  
    cout << "Enter a grade: ";
    cin >> grade;
    if (grade < 0){
        programEnd = 1;
    }
  
    // Computation
    sum += grade;
    count += 1;
    average = sum / count;
  
    cout << "Current grade total is: " << sum << endl;
    cout << "Current number of grades: " << count << endl << endl;
    cout << "Current Average is: " << average << endl << endl << endl;
}

main(){
    cout << "The Program will end if you input a NEGATIVE GRADE" << endl << endl;
  
    for(programEnd; programEnd < 1;){
        Func1();
    }      
}
masyadong complicated para sa easy problem.
 
Status
Not open for further replies.

Similar threads

Back
Top