I am a new C++ beginner, pls help~~~~

I am a new C++ beginner, which without any programming background.Now I am taking a c++/OOP course, and having some problem with my assignment. :<

The Assignment is :

try to create a C++ program that can check thet lottery result and the money that you win.
The followings are the requirements:

1. There are three functions in your program:
//Get the 6 numbers drawn and return the pointer to the main()
int *getDrawnNumbers();
//Get the 6 numbers bought and return the pointer to the main()
int *getBoughtNumbers();
//The function checks how many numbers you get correct and the amount of money you win
void analysis(int *drawNumbers, int *boughtNumbers);

2. The money for matching numbers listed in the following table:

Number Match, Money Win
1 and 2, No money win
3, $20
4, $320
5, $30000
6, $1 Million



Is anybody able to help me with this assignment ?
I dont know where is begin with :<
Pls help




Comments


  • Begin here:
    [code]
    int main () {

    return 0;
    }
    [/code]
    Serously, try it out yourself first before asking us to do it for you. I would be glad to help, but only after you have shown some effort yourself first.

    If you do have a more specific question, please feel free to ask.
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • First, thx for the reply, and a Happy New Year for all of u here :D,
    I didnt post anything because I am still at the halfway with my program,
    but anyway this is what I have so far, but I got error when I execute this:


    [code]

    #include "stdafx.h"
    #include
    #include
    #include
    #include "Lottery_Checker.h"

    int *getDrawnNumbers(); //function prototype
    int *getBoughtNumbers(); //function prototype

    int main()
    {
    int *drawnNumbers;
    int *boughtNumbers;
    drawnNumbers=getDrawnNumbers(); //call function int *getDrawnNumbers()
    boughtNumbers=getBoughtNumbers(); //call function int *getBoughtNumbers()

    return 0;
    }

    //Get the 6 numbers drawn and return the pointer to the main()
    int *getDrawnNumbers(){
    int DrawnNumbers[5]; //decalre an array
    cout <<"Please input drawn Number 1 -->";
    cin >> DrawnNumbers[0];
    cout <<"Please input drawn Number 2 -->";
    cin >> DrawnNumbers[1];
    cout <<"Please input drawn Number 3 -->";
    cin >> DrawnNumbers[2];
    cout <<"Please input drawn Number 4 -->";
    cin >> DrawnNumbers[3];
    cout <<"Please input drawn Number 5 -->";
    cin >> DrawnNumbers[4];
    cout <<"Please input drawn Number 6 -->";
    cin >> DrawnNumbers[5];
    cout<<endl;

    int *getDrawnNumbers; //declare pointer *getDrawnNumbers
    getDrawnNumbers=&DrawnNumbers[0]; //pointer *getDrawnNumbers points to DrawNumbers[0]
    return getDrawnNumbers; //return the pointer to the main()
    }

    //Get the 6 numbers you bought and return thet pointer to the main()
    int *getBoughtNumbers(){
    int BoughtNumbers[5]; //decalre an array
    cout <<"Please input Number 1 that you bought -->";
    cin >> BoughtNumbers[0];
    cout <<"Please input Number 2 that you bought -->";
    cin >> BoughtNumbers[1];
    cout <<"Please input Number 3 that you bought -->";
    cin >> BoughtNumbers[2];
    cout <<"Please input Number 4 that you bought -->";
    cin >> BoughtNumbers[3];
    cout <<"Please input Number 5 that you bought -->";
    cin >> BoughtNumbers[4];
    cout <<"Please input Number 6 that you bought -->";
    cin >> BoughtNumbers[5];
    cout<<endl;

    int *getBoughtNumbers; //declare pointer *getBoughtNumbers
    getBoughtNumbers=&BoughtNumbers[0]; //pointer *getBoughtNumbers points to BoughtNumbers[0]
    return getBoughtNumbers; //return the pointer to the main()
    }

    [/code]
  • I have corrected some of my code, and it works fine now for part 1 and 2.

    But I am still no idea what should I do for part 3, which require me to "checks how many numbers you get correct and the amount of money you win"

    hmmm....I am still think, anyone have any clue?? :D

    this is the code for part 1 and 2:

    [code]
    #include "stdafx.h"
    #include
    #include
    #include
    #include "Lottery_Checker.h"

    int *getDrawnNumbers(); //function prototype
    int *getBoughtNumbers(); //function prototype

    int main()
    {
    int *drawnNumbers;
    int *boughtNumbers;
    drawnNumbers=getDrawnNumbers(); //call function int *getDrawnNumbers()
    boughtNumbers=getBoughtNumbers(); //call function int *getBoughtNumbers()

    return 0;
    }

    //Get the 6 numbers drawn and return the pointer to the main()
    int *getDrawnNumbers(){
    int *getDrawnNumbers; //declare pointer *getDrawnNumbers
    getDrawnNumbers=(int *)malloc(5*sizeof(int)); //allocate memory
    int DrawnNumbers[5]; //decalre an array
    getDrawnNumbers=&DrawnNumbers[0]; //pointer *getDrawnNumbers points to DrawNumbers[0]

    cout <<"Please input drawn Number 1 -->";
    cin >> DrawnNumbers[0];
    cout <<"Please input drawn Number 2 -->";
    cin >> DrawnNumbers[1];
    cout <<"Please input drawn Number 3 -->";
    cin >> DrawnNumbers[2];
    cout <<"Please input drawn Number 4 -->";
    cin >> DrawnNumbers[3];
    cout <<"Please input drawn Number 5 -->";
    cin >> DrawnNumbers[4];
    cout <<"Please input drawn Number 6 -->";
    cin >> DrawnNumbers[5];
    cout<<endl;

    return getDrawnNumbers; //return the pointer to the main()
    }

    //Get the 6 numbers you bought and return thet pointer to the main()
    int *getBoughtNumbers(){
    int *getBoughtNumbers; //declare pointer *getBoughtNumbers
    getBoughtNumbers=(int *)malloc(5*sizeof(int)); //allocate memory
    int BoughtNumbers[5]; //decalre an array
    getBoughtNumbers=&BoughtNumbers[0]; //pointer *getBoughtNumbers points to BoughtNumbers[0]
    cout <<"Please input Number 1 that you bought -->";
    cin >> BoughtNumbers[0];
    cout <<"Please input Number 2 that you bought -->";
    cin >> BoughtNumbers[1];
    cout <<"Please input Number 3 that you bought -->";
    cin >> BoughtNumbers[2];
    cout <<"Please input Number 4 that you bought -->";
    cin >> BoughtNumbers[3];
    cout <<"Please input Number 5 that you bought -->";
    cin >> BoughtNumbers[4];
    cout <<"Please input Number 6 that you bought -->";
    cin >> BoughtNumbers[5];
    cout<<endl;

    return getBoughtNumbers; //return the pointer to the main()
    }
    [/code]

  • : I have corrected some of my code, and it works fine now for part 1
    : and 2.
    :
    : But I am still no idea what should I do for part 3, which require me
    : to "checks how many numbers you get correct and the amount of money
    : you win"
    :
    : hmmm....I am still think, anyone have any clue?? :D
    :
    : this is the code for part 1 and 2:
    :
    : [code]:
    : #include "stdafx.h"
    : #include
    : #include
    : #include
    : #include "Lottery_Checker.h"
    :
    : int *[color=Red]G[/color]etDrawnNumbers(); //function prototype
    : int *[color=Red]G[/color]etBoughtNumbers(); //function prototype
    :
    : int main()
    : {
    : int *drawnNumbers;
    : int *boughtNumbers;
    : drawnNumbers=[color=Red]G[/color]etDrawnNumbers(); //call function int *getDrawnNumbers()
    : boughtNumbers=[color=Red]G[/color]etBoughtNumbers(); //call function int *getBoughtNumbers()
    :
    [color=Red]: [b]// #1: you should deallocate memory upon exit
    : // In this simple case nothing will happen if you do not do this,
    : // but this is just good coding practice.
    : //[/b]
    : free (drawnNumbers);
    : free (boughtNumbers);[/color]
    : return 0;
    : }
    :
    : //Get the 6 numbers drawn and return the pointer to the main()
    [color=Red]: // Why do you have the function name exactly the same as the
    : // Allocated array name? Compiler might get confused.
    : // I adjusted the function names to have the uppercase 'G', so
    : // they will differ from array names.[/color]
    : int *[italic][color=Red]G[/color]etDrawnNumbers[/italic](){
    : int *[italic]getDrawnNumbers[/italic]; //declare pointer *getDrawnNumbers
    : getDrawnNumbers=(int *)malloc([color=Red]6[/color]*sizeof(int)); //allocate memory
    : [b][color=Red]//
    : // You allocated only 5 elements ^^^ and you need six.
    : // If you look at the code below - you will see that
    : // indexes are: 0,1,2,3,4,5 - and that is SIX elements!
    : // If this code does not crash - it is only by chance.[/color][/b]
    :
    [color=Red]: [b]//int DrawnNumbers[5]; //decalre an array
    : //getDrawnNumbers=&DrawnNumbers[0]; //pointer *getDrawnNumbers points to DrawNumbers[0]
    : //
    : // Why doing that ^^^ strange pointing thing?
    : // No need to do that - simply use the name of the
    : // allocated array as a normal array - they are the same.
    : // See code below - I adjusted the names[/b].[/color]
    :
    : cout <<"Please input drawn Number 1 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][0];
    : cout <<"Please input drawn Number 2 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][1];
    : cout <<"Please input drawn Number 3 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][2];
    : cout <<"Please input drawn Number 4 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][3];
    : cout <<"Please input drawn Number 5 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][4];
    : cout <<"Please input drawn Number 6 -->";
    : cin >> [color=Red]getDrawnNumbers[/color][5];
    : cout<<endl;
    :
    : return getDrawnNumbers; //return the pointer to the main()
    : }
    :
    : //Get the 6 numbers you bought and return thet pointer to the main()
    : [b][color=Red]// I adjusted the code here too[/color][/b]
    : int *[color=Red]G[/color]etBoughtNumbers(){
    : int *getBoughtNumbers; //declare pointer *getBoughtNumbers
    : getBoughtNumbers=(int *)malloc([color=Red]6[/color]*sizeof(int)); //allocate memory
    [color=Red]: //int BoughtNumbers[5]; //decalre an array
    : //getBoughtNumbers=&BoughtNumbers[0]; //pointer *getBoughtNumbers points to BoughtNumbers[0][/color]
    : cout <<"Please input Number 1 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][0];
    : cout <<"Please input Number 2 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][1];
    : cout <<"Please input Number 3 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][2];
    : cout <<"Please input Number 4 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][3];
    : cout <<"Please input Number 5 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][4];
    : cout <<"Please input Number 6 that you bought -->";
    : cin >> [color=Red]getBoughtNumbers[/color][5];
    : cout<<endl;
    :
    : return getBoughtNumbers; //return the pointer to the main()
    : }
    : [/code]:
    :
    :
    [color=Blue]Now, all you need is analysis function. It is a simple nested loop, which checks one of array elements against ALL of the elements in OTHER array:
    [/color]
    [code]
    int GetMatches (int* arr1, int* arr2)
    {
    int nMatches = 0;
    int element;

    for (int iElement = 0; iElement < 6; iElement++)
    {
    element = arr1 [iElement];
    for (int iCheck = 0; iCheck < 6; iCheck++)
    {
    if (element == arr2 [iCheck])
    {
    nMatches++;
    }
    }
    }
    return nMatches;
    }
    [/code]
  • thx for the help, I have adjusted some of my code, and it worked,it really nice to have someone help me with this :D

  • : thx for the help, I have adjusted some of my code, and it worked,it
    : really nice to have someone help me with this :D
    :
    :
    This will be better in a loop:
    [code]
    cout <<"Please input drawn Number 1 -->";
    cin >> getDrawnNumbers[0];
    cout <<"Please input drawn Number 2 -->";
    cin >> getDrawnNumbers[1];
    cout <<"Please input drawn Number 3 -->";
    cin >> getDrawnNumbers[2];
    cout <<"Please input drawn Number 4 -->";
    cin >> getDrawnNumbers[3];
    cout <<"Please input drawn Number 5 -->";
    cin >> getDrawnNumbers[4];
    cout <<"Please input drawn Number 6 -->";
    cin >> getDrawnNumbers[5];
    [/code]
    To...
    [code]
    for (int i=0; i<6; i++) {
    cout <<"Please input drawn Number " << i+1 << " -->";
    cin >> getDrawnNumbers[i];
    }
    [/code]
    There is another location where this can be applied--try to see if you can find it.

    This loop not only will help increase the speed, and decrease size; but will also get rid of some duplicate code ;-)

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • : : thx for the help, I have adjusted some of my code, and it worked,it
    : : really nice to have someone help me with this :D
    : :
    : :
    : This will be better in a loop:
    : [code]:
    : cout <<"Please input drawn Number 1 -->";
    : cin >> getDrawnNumbers[0];
    : cout <<"Please input drawn Number 2 -->";
    : cin >> getDrawnNumbers[1];
    : cout <<"Please input drawn Number 3 -->";
    : cin >> getDrawnNumbers[2];
    : cout <<"Please input drawn Number 4 -->";
    : cin >> getDrawnNumbers[3];
    : cout <<"Please input drawn Number 5 -->";
    : cin >> getDrawnNumbers[4];
    : cout <<"Please input drawn Number 6 -->";
    : cin >> getDrawnNumbers[5];
    : [/code]:
    : To...
    : [code]:
    : for (int i=0; i<6; i++) {
    : cout <<"Please input drawn Number " << i+1 << " -->";
    : cin >> getDrawnNumbers[i];
    : }
    : [/code]:
    : There is another location where this can be applied--try to see if
    : you can find it.
    :
    : This loop not only will help increase the speed, and decrease size;
    : but will also get rid of some duplicate code ;-)
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    [color=Blue]
    Theoretically, loop (six times) is slower than sequence of these six statements, but of course, code size matters more here. The difference is a few clocks.

    This happens, because for CPU is easier to run through the straight line than do jumps.
    [/color]
  • I think most compilers (at least ones for embedded systems) support "loop unrolling" as an optimization feature. That is, the compiler will make a smart decision on whether it is best to optimize for speed or code size for every loop in the code.

    That is however an advanced topic, and the programmer doesn't need to concern himself with this, he will want to write loops, since loops are generally easier to read.

    (Unless they pay you per line of source code written :-))

    The only optimization he needs to concern himself with is whether the loop condition is a constant or not. Example:

    [code]/* bad, calls strlen over and over */

    for(int i=0; i<strlen(str); i++)
    {
    str[i] = something;
    }[/code]
    [code]/* good, calls strlen once */

    int length = strlen(str);

    for(int i=0; i<length; i++)
    {
    str[i] = something;
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories