🔒 Closed C++ problem

Status
Not open for further replies.

Kingrhejj

Enthusiast
Need help about c++ mga ka phc.

ayaw mag stop ng program
Sa E×ρréššion 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 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.

About this Thread

  • 40
    Replies
  • 2K
    Views
  • 9
    Participants
Last reply from:
siesta

Trending Topics

Online now

Members online
1,137
Guests online
995
Total visitors
2,132

Forum statistics

Threads
2,279,091
Posts
28,987,995
Members
1,226,717
Latest member
J3ss3
Back
Top