Hello guys, I found this exercise online and i need to do it for my class.
Write a program that implements the following diagram:
http://i66.tinypic.com/2h7nls2.png (link to the diagram)
Use new to allocate any memory wherever it can be used without incurring any memory leaks and will end up implementing the diagram. For the remainder, use already declared pointer variables where that must be done in order to implement the diagram. Thus there will be a mix where some pointers are dynamically allocated and others will simply be assigned addresses of existing variables. It is up to you to determine what a correct mix is.
The shaded boxes represent pointers. The unshaded boxes represent a dynamically allocated two dimensional int array. Your program cannot create named identifiers for any of the unnamed items. The number of rows and the number of columns will be user input. The ellipses in the diagram represent that the sizes of the rows and columns are variable. The vertical array is an array of pointers. Each of the vertical array elements points to a one dimensional array of integers. This is really what a two dimensional array is. While each integer array could be a different length (number of columns), that is a ragged array, for this project we will use a rectangular array. The box (pointer) pointed to by u and v points to the first element of the first row array.
In your program only refer to the integer array through the pointers represented by s and t. u and v will be used as per the example program output. When passing to functions you must always pass these pointers and use these identifiers in the functions. You cannot dereference actual parameters in a function call. Nor can you dereference pointers and assign them to variables just in order to work with them.
The “Memory Layout for C++ Part 2” document discusses levels of indirection. Each arrow in the above diagram represents a level of indirection. Each level of indirection corresponds to an “*” when using a pointer variable.
Further, your program is to have, at a minimum:
• An allocation function, called from main, which allocates and assigns all the memory. That is, the entire implementation of the diagram is in this function. This function will also get the values for rows and columns from the user.
• An input function, called by main. For accessing the array, the function must use only pointer/offset notation. Run TopicA.exe to see what this is supposed to look like. Remember to take operator precedence into account.
• A function to reverse the contents of each row. Called from main. For accessing the array, the function must use only array subscript notation. Remember to take operator precedence into account. If necessary, a separate array can be created to perform the reversal, although this is inefficient and unnecessary. This separate array must use the array class template. (The Extra Credit uses only one array.)
• A function to display the array. Called from main.
• A function to deallocate memory. Called from main
• No global variables.
• Appropriate use of & and const.
• Use C++ 11 uniform initialization where appropriate.
Run the example program TopicA.exe. The “Press Enter to end” prompt is part of your program to implement. (“Press any key to continue” is from Visual Studio and cannot be taken out.) Call your source code file TopicA.cpp.
Extra Credit (10 points)
As noted above, using a separate array to implement the reversal is inefficient and unnecessary. It is inefficient because a separate array must be created, the values copied into the array, and then the values copied back into the source array. The way this is usually done is to use the source array and reverse the values using only the source array. This way no extra array is created. The extra credit is to do it using only the source array. I suggest that you first work out the algorithm by hand using pencil and paper. The program status must state that it is the extra credit version in order to get the extra credit.
Here is what i have so far :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
//Project A c++
#include <iostream>
#include <string>
using namespace std;
int alloc();
int input();
int reverse();
int display();
int dealloc();
int main()
{
}
int alloc()
{
int j = 0, p = 0;
int ***s, ***t, **u, **v;
cout << "How Many Rows Should The Array Have ? (At least 2) --> ";
cin >> j;
cout << "How many columns should the array have ? (At least 2) --> ";
cin >> p;
s = new int**;
s = t;
*s = new int*;
**s = new int[j];
u = new int*;
u = v;
*u = new int[p];
**s = new int*[p];
for (int e = 0; e <= j; e++)
{
*(s + e) = new int[p];
}
return 0;
}
int input()
{
return 0;
}
int reverse()
{
return 0;
}
int display()
{
return 0;
}
int dealloc()
{
return 0;
}
|
Some help?