What's new

Closed Basic c++ Programming problems patulong please 1week ko na di magawa hirap

Status
Not open for further replies.
View attachment 683864Ganto kasi eto pinapagawa, dapat maga input ako change. Eto problem, like sa example ni sir mag input ako nang ganto
View attachment 683866
478php. Dapat ang output nya is if ilan na 200 papel meron, ilan 100papel meron, etc. Ganyan. Pero eto sa codes ko hirap mapa ganyan ang lumalabas aa codes ko eto tingnan nyo :(

#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
A:
system("cls");
float inputNum;
int hundred5,hundred2, hundred1, fifty, twenty, ten, five, one;
int tfc,tc,fc;
cout<<"PREFINAL PROJECT 2"<<endl<<endl;
cout<<"Enter the Change: ";
cin>>inputNum;
hundred5 = inputNum/500;
inputNum -= (hundred5 * 500);
hundred2 = inputNum/200;
inputNum -= (hundred2 * 200);
hundred1 = inputNum/100;
inputNum -= (hundred1 * 100);
fifty = inputNum/50;
inputNum -= (fifty * 50);
twenty = inputNum/20;
inputNum -= (twenty * 20);
ten = inputNum/10;
inputNum -= (ten * 10);
five = inputNum/5;
inputNum -= (five * 5);
one = inputNum/1;
inputNum -= (one * 1);
tfc = inputNum/.25;
inputNum -= (tfc * .25);
tc = inputNum/.10;
inputNum -= (tc * .10);
fc = inputNum/.05;
inputNum -= (fc * .05);
cout<<hundred5;
cout<<" 500 peso bill"<<endl;
cout<<hundred2;
cout<<" 200 peso bill"<<endl;

cout<<hundred1;
cout<<" 100 peso bill"<<endl;
cout<<fifty;
cout<<" 50 peso bill"<<endl;
cout<<twenty;
cout<<" 20 peso bill"<<endl;
cout<<ten;
cout<<" 10 peso coin"<<endl;
cout<<five;
cout<<" 5 peso coin"<<endl;
cout<<one;
cout<<" 1 peso coin"<<endl;
cout<<tfc;
cout<<" 25 centavo coin"<<endl;
cout<<tc;
cout<<" 10 centavo coin"<<endl;
cout<<fc;
cout<<" 5 centavo coin"<<endl<<endl<<endl;
cout<<"Another [Y/N] : ";
char ans; cin>>ans;
if (ans =='Y' || ans == 'y'){
goto A;
}
return 0;
}

Eto lumalabas
View attachment 683868
Tama na codes ko sana, pero dapat hindi yan lalabas na ibang 0 naman, like 0 pcs of 100 peso bill, 0pcs 10 peso coin like dun sa pics na una na example ni sir if else ba dapat gamitin ko para di yab lalabas na 0pcs sa output :(
since prefinals na. loop dapat gamitin mo and arrays
tama mas iiksi yan.,
 
Yung loop ay isang issue lang. Ang mas malaking issue dito ay paano mo ia-associate yung value into their corresponding string, ie 500:"500 hundered peso bill". Without that association, you are going to have a hard time displaying quantities and their respective string-value.

Isang solusyon ko dito ay using struct:
Code:
#include <iostream>

struct mystruct {
    char name[30];
    int quantity;
    const float value;
};


int main(void) {
    float change{0};
    std::cout << "Enter change: " << std::flush;
    std::cin >> change;

    mystruct five_hundred {
        "500 hundred peso bill",
        0,
        500
    };

    mystruct two_hundred {
        "200 hundred peso bill",
        0,
        200
    };

    mystruct one_hundred {
        "100 hundred peso bill",
        0,
        100
    };

    mystruct fifty {
        "50 peso bill",
        0,
        50
    };

    mystruct twenty {
        "20 peso bill",
        0,
        20
    };

    mystruct ten {
        "10 peso coin",
        0,
        10
    };

    mystruct five {
        "5 peso coin",
        0,
        5
    };

    mystruct one {
        "1 peso coin",
        0,
        1
    };

    mystruct twenty5cents {
        "25 centavo coin",
        0,
        0.25
    };

    mystruct tencents {
        "10 centavo coin",
        0,
        0.10
    };

    mystruct fivecents {
        "5 centavo coin",
        0,
        0.05
    };

    mystruct structarray[] { 
        five_hundred, two_hundred, one_hundred, fifty, twenty, ten, five, one, twenty5cents, tencents, fivecents
    };

    std::cout << std::endl;
    int num_of_denominations = sizeof(structarray)/(sizeof(*structarray));
    for (int i=0; i<num_of_denominations; i++) {
        structarray[i].quantity = change/structarray[i].value;
        if (structarray[i].quantity > 0) {
            std::cout << structarray[i].name << "\t\t= " << structarray[i].quantity << std::endl;
        }
        change -= (structarray[i].quantity * structarray[i].value);

    }
    return 0;
}
 
Yung loop ay isang issue lang. Ang mas malaking issue dito ay paano mo ia-associate yung value into their corresponding string, ie 500:"500 hundered peso bill". Without that association, you are going to have a hard time displaying quantities and their respective string-value.

Isang solusyon ko dito ay using struct:
Code:
#include <iostream>

struct mystruct {
    char name[30];
    int quantity;
    const float value;
};


int main(void) {
    float change{0};
    std::cout << "Enter change: " << std::flush;
    std::cin >> change;

    mystruct five_hundred {
        "500 hundred peso bill",
        0,
        500
    };

    mystruct two_hundred {
        "200 hundred peso bill",
        0,
        200
    };

    mystruct one_hundred {
        "100 hundred peso bill",
        0,
        100
    };

    mystruct fifty {
        "50 peso bill",
        0,
        50
    };

    mystruct twenty {
        "20 peso bill",
        0,
        20
    };

    mystruct ten {
        "10 peso coin",
        0,
        10
    };

    mystruct five {
        "5 peso coin",
        0,
        5
    };

    mystruct one {
        "1 peso coin",
        0,
        1
    };

    mystruct twenty5cents {
        "25 centavo coin",
        0,
        0.25
    };

    mystruct tencents {
        "10 centavo coin",
        0,
        0.10
    };

    mystruct fivecents {
        "5 centavo coin",
        0,
        0.05
    };

    mystruct structarray[] {
        five_hundred, two_hundred, one_hundred, fifty, twenty, ten, five, one, twenty5cents, tencents, fivecents
    };

    std::cout << std::endl;
    int num_of_denominations = sizeof(structarray)/(sizeof(*structarray));
    for (int i=0; i<num_of_denominations; i++) {
        structarray[i].quantity = change/structarray[i].value;
        if (structarray[i].quantity > 0) {
            std::cout << structarray[i].name << "\t\t= " << structarray[i].quantity << std::endl;
        }
        change -= (structarray[i].quantity * structarray[i].value);

    }
    return 0;
}
ayos
 
Sir ano po yan na iba diko gets if else palang tinuturo samin 1st yr college po wala pa tinuro samin na ganyan struct at iba sa codes mo. Hehehe pede ko bayun gawin na if else kaso pano
Yung loop ay isang issue lang. Ang mas malaking issue dito ay paano mo ia-associate yung value into their corresponding string, ie 500:"500 hundered peso bill". Without that association, you are going to have a hard time displaying quantities and their respective string-value.

Isang solusyon ko dito ay using struct:
Code:
#include <iostream>

struct mystruct {
    char name[30];
    int quantity;
    const float value;
};


int main(void) {
    float change{0};
    std::cout << "Enter change: " << std::flush;
    std::cin >> change;

    mystruct five_hundred {
        "500 hundred peso bill",
        0,
        500
    };

    mystruct two_hundred {
        "200 hundred peso bill",
        0,
        200
    };

    mystruct one_hundred {
        "100 hundred peso bill",
        0,
        100
    };

    mystruct fifty {
        "50 peso bill",
        0,
        50
    };

    mystruct twenty {
        "20 peso bill",
        0,
        20
    };

    mystruct ten {
        "10 peso coin",
        0,
        10
    };

    mystruct five {
        "5 peso coin",
        0,
        5
    };

    mystruct one {
        "1 peso coin",
        0,
        1
    };

    mystruct twenty5cents {
        "25 centavo coin",
        0,
        0.25
    };

    mystruct tencents {
        "10 centavo coin",
        0,
        0.10
    };

    mystruct fivecents {
        "5 centavo coin",
        0,
        0.05
    };

    mystruct structarray[] {
        five_hundred, two_hundred, one_hundred, fifty, twenty, ten, five, one, twenty5cents, tencents, fivecents
    };

    std::cout << std::endl;
    int num_of_denominations = sizeof(structarray)/(sizeof(*structarray));
    for (int i=0; i<num_of_denominations; i++) {
        structarray[i].quantity = change/structarray[i].value;
        if (structarray[i].quantity > 0) {
            std::cout << structarray[i].name << "\t\t= " << structarray[i].quantity << std::endl;
        }
        change -= (structarray[i].quantity * structarray[i].value);

    }
    return 0;
}
ir
 
Sir ano po yan na iba diko gets if else palang tinuturo samin 1st yr college po wala pa tinuro samin na ganyan struct at iba sa codes mo. Hehehe pede ko bayun gawin na if else kaso pano

ir
Kung if-else palang kayo, dapat maunti lang muna conditions, mga 3-4 lang. Saka sa code mo, minsan lang nabanggit yung if statement (at line 62):
Code:
user@debian10:~/programming/cpp/garbage$ grep -n '\<if\>' change-orig.cpp
62:if (ans =='Y' || ans == 'y'){

Pwedeng gawin sa if-else? Pwede, asahan mo nga lang na magiging kasing-haba ng train yung if-else block mo.

Next time, embed mo sa code-blocks yung code mo para maayos tingnan.
 
Last edited:
Sir ganto po ba tama?

if ( change < 500) {
cout<<" 500 peso bill"<<hundred5<<endl;

Ganyab po ba like sa 500,hindi naba lalabas mga 0 pcs sa output?


Kung if-else palang kayo, dapat maunti lang muna conditions, mga 3-4 lang. Saka sa code mo, minsan lang nabanggit yung if statement (at line 62):
Code:
user@debian10:~/programming/cpp/garbage$ grep -n '\<if\>' change-orig.cpp
62:if (ans =='Y' || ans == 'y'){

Pwedeng gawin sa if-else. Pwede, asahan mo nga lang na magiging kasing-haba ng train yung if-else block mo.

Next time, embed mo sa code-blocks yung code mo para maayos tingnan.
ir
 
Sir ganto po ba tama?

if ( change < 500) {
cout<<" 500 peso bill"<<hundred5<<endl;

Ganyab po ba like sa 500,hindi naba lalabas mga 0 pcs sa output?



ir
Say, we have this value: 501. Challenge pano mo idi-display, katulad nito

Code:
user@debian10:~/programming/cpp/garbage$ c++ mychange.cpp && ./a.out
Enter change: 501

500 hundred peso bill        = 1
1 peso coin        = 1

Yun yung binabanggit ko sa itaas na kelangan i-associate mo yung 500:"500 peso hundred bill". Ngayun kung walang association (eg, key:value, map o even something rudimentary), katakut-takot na if-else yan na apart from inelegant is also logically incorrect.
 
Possible answer using if else and while loop only. No arrays.


#include <iostream>
#include <string>
using namespace std;
int main() {
float total = 1975.15;
float _1k = 0, _5h = 0, _2h = 0, _1h = 0, _50p = 0, _20p = 0, _10p = 0, _5p = 0, _1p = 0, _25c = 0, _10c = 0, _5c = 0;
while (total > 0) {
if (total >= 1000) {
total -= 1000;
_1k += 1;
} else if (total >= 500) {
total -= 500;
_5h += 1;
} else if (total >= 200) {
total -= 200;
_2h += 1;
} else if (total >= 100) {
total -= 100;
_1h += 1;
} else if (total >= 50) {
total -= 50;
_50p += 1;
} else if (total >= 20) {
total -= 20;
_20p += 1;
} else if (total >= 10) {
total -= 10;
_10p += 1;
} else if (total >= 5) {
total -= 5;
_5p += 1;
} else if (total >= 1) {
total -= 1;
_1p += 1;
} else if (total >= .25) {
total -= .25;
_25c += 1;
} else if (total >= .10) {
total -= .10;
_10c += 1;
} else if (total >= .05) {
total -= .05;
_5c += 1;
}
}
if(_1k > 0) cout << _1k << " - 1000 peso" << endl;
if(_5h > 0) cout << _5h << " - 500 peso" << endl;
if(_2h > 0) cout << _2h << " - 200 peso" << endl;
if(_1h > 0) cout << _1h << " - 100 peso" << endl;
if(_50p > 0) cout << _50p << " - 50 peso" << endl;
if(_20p > 0) cout << _20p << " - 20 peso" << endl;
if(_10p > 0) cout << _10p << " - 10 peso" << endl;
if(_5p > 0) cout << _5p << " - 5 peso" << endl;
if(_1p > 0) cout << _1p << " - 1 peso" << endl;
if(_25c > 0) cout << _25c << " - 25 cents" << endl;
if(_10c > 0) cout << _10c << " - 10 cents" << endl;
if(_5c > 0) cout << _5c << " - 5 cents" << endl;
}
 
Possible answer using if else and while loop only. No arrays.


#include <iostream>
#include <string>
using namespace std;
int main() {
float total = 1975.15;
float _1k = 0, _5h = 0, _2h = 0, _1h = 0, _50p = 0, _20p = 0, _10p = 0, _5p = 0, _1p = 0, _25c = 0, _10c = 0, _5c = 0;
while (total > 0) {
if (total >= 1000) {
total -= 1000;
_1k += 1;
} else if (total >= 500) {
total -= 500;
_5h += 1;
} else if (total >= 200) {
total -= 200;
_2h += 1;
} else if (total >= 100) {
total -= 100;
_1h += 1;
} else if (total >= 50) {
total -= 50;
_50p += 1;
} else if (total >= 20) {
total -= 20;
_20p += 1;
} else if (total >= 10) {
total -= 10;
_10p += 1;
} else if (total >= 5) {
total -= 5;
_5p += 1;
} else if (total >= 1) {
total -= 1;
_1p += 1;
} else if (total >= .25) {
total -= .25;
_25c += 1;
} else if (total >= .10) {
total -= .10;
_10c += 1;
} else if (total >= .05) {
total -= .05;
_5c += 1;
}
}
if(_1k > 0) cout << _1k << " - 1000 peso" << endl;
if(_5h > 0) cout << _5h << " - 500 peso" << endl;
if(_2h > 0) cout << _2h << " - 200 peso" << endl;
if(_1h > 0) cout << _1h << " - 100 peso" << endl;
if(_50p > 0) cout << _50p << " - 50 peso" << endl;
if(_20p > 0) cout << _20p << " - 20 peso" << endl;
if(_10p > 0) cout << _10p << " - 10 peso" << endl;
if(_5p > 0) cout << _5p << " - 5 peso" << endl;
if(_1p > 0) cout << _1p << " - 1 peso" << endl;
if(_25c > 0) cout << _25c << " - 25 cents" << endl;
if(_10c > 0) cout << _10c << " - 10 cents" << endl;
if(_5c > 0) cout << _5c << " - 5 cents" << endl;
}
Lupet ng codes mo sir try ko maya sa dev++. Kaso anong yung float total =1975.15 ano yan. Tsaka 500 lang po limit walang 1k
 
pano ma istuck? unless decimal mo is equal to non divisible by .05
Sinubukan mo ba i-compile and run code mo?
stuck.png
 

Attachments

Examining the inputs here I can say that this is the number to words problem. I wonder kung bakit hanggang ngayon may mga hindi parin maka gets sa gasgas na programming exercise 'to.

This is my code using codeblocks with visual C++ compiler used but you can run it on online c++ compiler too using this link: You do not have permission to view the full content of this post. Log in or register now.

It's originally from my C# exams in my College days way back 2009. I just rewritten it to C++ equivalent so wag maghanapng best practice code ng C++ kasi hindi ako hardcore sa language na yan. Hobbyist lang kasi kasama yan sa mga technique para mapanatili ko yung skills ko sa mga C-Like languages.

This can translate up to 1 Million integer.

C++:
#include <iostream>
#include <string>

using namespace std;

std::string IntegerToWords(int number)
{
            string inWords = "";
            string Units[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
            string Tens[10] = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

            if (number == 0)
            {
                return "Zero";
            }

            if (number < 0)
            {
                return "Negative " + IntegerToWords(std::abs(number));
            }

            inWords = "";

            if ((number / 1000000) > 0)
            {
                inWords += IntegerToWords(number / 1000000) + " Million ";
                number %= 1000000;
            }

            if ((number / 1000) > 0)
            {
                inWords += IntegerToWords(number / 1000) + " Thousand ";
                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                inWords += IntegerToWords(number / 100) + " Hundred ";
                number %= 100;
            }

            if (number > 0)
            {
                if (number < 20)
                {
                    inWords += Units[number];
                }
                else
                {
                    inWords += Tens[number / 10];
                    if ((number % 10) > 0)
                    {
                        inWords += "-" + Units[number % 10];
                    }
                }
            }

            return inWords;
}



int main()
{
    int sukli = 0;
    string sukliInWords = "";

    std::cout << "Enter Sukli :";
    std::cin >> sukli;

    sukliInWords = IntegerToWords(sukli);

    std::cout << sukliInWords << std::endl;
}

It's doesnt output exactly what is asking without the centavo thing pero it translate the whole number to its words equivalent. madali na lang incorporate yung decimal point nyan para makuha din pati centavo kung imodify konti yung code.
 
Last edited:
Status
Not open for further replies.
Back
Top