Click here to Skip to main content
15,566,994 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is the part after the first for loop where moTotal is assigned the value of the compute_sales_total function. when I run and input the last month in the 10 years, it crashes. I'm not sure if it's how i'm trying to pass it or if it's how things are named or where they are.. I'm new to this whole passing 2 dimensional arrays to functions thing. should I be using vectors cause we haven't talked about them at all in class yet.

C++
#include <iostream>
using namespace std;

const int years = 10, months = 12;

int read_sales_data();
int *compute_sales_total(int[][months], int);
//int swap(int, int);   don't worry about these 3 yet I'm sure I could             figure them out after I get around this other problem.
//int sort_sales_total(int, int);
//void output_sales_data(int);

int main()
{
    int yrMo[years][months], *moTotal;
    char run;

    cout<< "Would you like to run this program? Enter Y/y or N/n.\n";
    cin >> run;

    while(run == 'Y' || run == 'y')
    {
        for(int i = 0; i < years; i++)
        {
            for(int n = 0; n < months; n++)
            {
                cout<< "Year " << i+1 << ".\nEnter sales for month " << n+1 << " = ";
                yrMo[i][n] = read_sales_data();
            }
        }

        moTotal = compute_sales_total(yrMo, years);

        for(int a=0; a < months; a++)
        {
            cout<< "Month " << a+1 << " totals were " << *(moTotal + a) << "\n";
        }

        cout<< "\nWould you like to run this program again? Enter Y/y or N/n.\n";
        cin>> run;
    }
}

    int read_sales_data()
    {
        int x = 0;
        cin>> x;
        system("cls");

        return x;
    }

    int *compute_sales_total(int yrMo[][months], int years)
    {
        int *moTot = 0;
        for(int i=0; i < months; i++)
        {
            for(int n=0; n < years; n++)
            {
                *(moTot + i) += yrMo[n][i];
            }
        }
        return moTot;
    }
Posted
Updated 3-Mar-12 21:47pm
v4

This is not an answer, just a rant for the teachers:

This guy is what you are candidate to be one of the last C++ BAD PROGRAMMES.
Why? Look what he said:

should I be using vectors cause we haven't talked about them at all in class yet.

Believing or not, this is the mother of all memory leaks.

It is 2012. C++ was created in the late '90s, standardized in 2003 and standardized again in 2011.

How long shold we wait before you in your classrooms will start to teach C++ by properly respect its standardization and design?

Should the result of 15 years of standardization be just this? Should it be you, continuing to fulminate the brain of newbies with pointers and pointer arithmetic writing exercise impossible to understand if erroneous, since there is nothing inside them that can EXPLAIN (very different from SHOW) what error is in there?

How long we must wait before all C++ teachers around the world will stop teaching bad practice and then - only in the last 10 minutes of their very last lesson - say "oh ... and there is also the standard library".

Do you understand you're completely missing up the work of 10 years of standardization?

Make a little autocritic: If you teach to use int-s and related operation as they are primitives (and they are not: bits are primitives), what is actually missing in your brains to make the contact that "yes, also std::string-s behaves as primitives" (They are "carrier of values for themselves"). And std::vector are no different.

STL containers are not "something exotic that specialist will learn in "advanced courses". They are part of A SAME ISO SPECIFICATION. The same of the C++ language.
The language and its standard library are part of a SAME CONSTRUCT.
Otherwise, for coherence, if you consider container "advanced topics" for what reason you don't consider streams tyhe same?

Just look at what a std::stream is: it has to work with buffers and locales. It has to do so many thigs... but 10+ years ago a guy name Stroustroup wrote "cout << "hello world"" and magically they became primitive.
If you don't consider srings or vectors as primitives, you should not consider streams as well, and manage input and output with the C library. Do you get the point? You are not teaching C++, just C w/ classes and streams.

The proper way to teach C++ is introducing types as:

- Numbers (from char to long long, to float and double-s)
- Text (strings and streams)
- Aggregates (vectors, lists, set, map ...)

Them moving towards

- composite types (classes)
- objects (class and virtual functions), and aggregations (embedding and inheritance)
- static (template) and dynamic (inheritance) polymorphism

Then starting to talk about patterns.
And introducing the just necessary low level stuff (pointer and reference) to proper manage and handle functions.

Low level stuffs like pointer arithmetic, array etc. should not be taught but the last part of the course, when start teaching about how the inside of the library itself is done, and how the library itself can be extended or other libraries created.

It wold be interesting this answer to be posted to teachers as well and see what they think about.
 
Share this answer
 
v2
Comments
Nelek 4-Mar-12 6:15am    
5 from me.

And maybe would it be worthy to write a "teachers-oriented" article
Maximilien 4-Mar-12 12:59pm    
Hear-Hear
(see the thread I created a couple of days ago : http://www.codeproject.com/Lounge.aspx?msg=4172152#xx4172152xx)
Emilio Garavaglia 4-Mar-12 14:56pm    
I saw the thread and the way it was mostly misunderstood.

There's nothing bad teaching C, where C is required.
The bad thing is teaching C where C++ is required, sowing as "C++" what is just C.

Low level stuff is something that has top be known. The problem is "when".
The key is in
C++
int *compute_sales_total(int yrMo[][months], int years)
{
    int *moTot = 0;
    for(int i=0; i < months; i++)
    {
        for(int n=0; n < years; n++)
        {
            *(moTot + i) += yrMo[n][i];
        }
    }
    return moTot;
}


moTot is a pointer to int that is initialize (the pointer) as null.
so moTot+i points to an arbitrary memory outside your program space, and you're trying to write values in there.

Think a while what the logic of moTot should be and redesign.
 
Share this answer
 
Comments
EndlessMike 4-Mar-12 10:01am    
Could you explain a little better without giving away the answer or am I close, because I stared at that for about 2 hours yesterday changing the way I passed the 2d array and passed back the pointer...
Emilio Garavaglia 4-Mar-12 12:19pm    
The point is that moTot has NO MEMORY associated with it.
You can:
- allocate when needed (but you have to delete it when no more needed ...)
- declare a return array in main, and pass it to be written as a third parameter (and not use the return value)
- return a class that manages the memory itself (like std::vector): thta's teh proper design, but frankly, with the teachers you have, doing the right way will be impossible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900