Pointer and Dynamically Allocated Array

I was working on this program for 2 days. I'm a beginner and i need some help please.

CS M10B - Topic A Project –
Due Wednesday 9/10

Write a program that implements the following diagram:
The diagram is available in this link: http://i59.tinypic.com/v8mtsk.png


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 the correct mix is. Your program cannot create named identifiers for any unnamed boxes.

The shaded boxes represent pointers. The unshaded horizontal box represents an array of pointers to int. The unshaded vertical arrays represent int arrays of unknown size (as indicated by the vertical ellipses). The size for the first two (starting from the left) are obtained indirectly from the user and are to be dynamically allocated. The rightmost array size will be determined by the program as given below and dynamically allocated.

In fact what is represented is a two dimensional ragged array. That is, the horizontal array represents the first dimension, the rows, while the vertical arrays represent the columns. It is called a ragged array as each column can have a different length. This way of representing a two dimensional array is, in fact, the way that C++ actually handles multidimensional arrays.

In order to capture the sizes of all three of the vertical (column) arrays, you will create an array in main which will hold the array sizes. Element 0 will have the size of the first array, element 1 the second array's size, and element 2 the third size. Use the array class template.

Your program should ask the user for 2 non-negative integer values. Be sure to verify that the values are correct. The program will then allocate the first two arrays where the size of each is the number of digits in the respective values. Each digit is to be put into a separate element. Finally, the last array is to be the size of the larger of the two other arrays. Digits will be added across and the sum stored in the corresponding element of the last array. For example, the value in index 0 in the first (leftmost) array is added to the value in index 0 in the second array. That sum is stored in index 0 in the last array. Run the example program to see what is required. Notice that if there is a carry, the number shown has two digits. (Try values such as 175 and 27 to see what happens.) However, see the extra credit below.

In your program only refer to the integer array through the pointers represented by x, y, and z. That is, do not use q when referring to an actual integer value or reduce the number of levels of indirection when using parameters or substitute any other variables.

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 these functions, at a minimum:
• The allocation function, called from main, which allocates and assigns all the memory except for the 3 lower (column) arrays and the sizes array.
• The input function, called by main. This input function then calls the function which checks to be sure that the input is really a non-negative integer. Calls the function to put the values into the first and second arrays. The input function also puts the sizes of the first two arrays into the sizes array. Use x as the parameter to access the array and use array notation for accessing the arrays.
• The number checking function verifies if that value is a valid non-negative integer. Called by the input function.
• The function to allocate and put an input value into one of the vertical arrays. Called by the input function.
• The summation function, called by main, does the summation. The function will do the cross sums. Use only the arrays, not any other values, such as the original input numbers. Allocates the last array and puts its size into the sizes array. Use y as the parameter to access the array and use pointer (address) notation when using y. (Address notation cannot be used with arrays allocated using the array class template.)
• A display function to display the results. Use z as the parameter to access the array and use pointer (address) notation when using z.
• Remember – no global variables

If the actual parameters for functions are pointers they should not be dereferenced. That is, if y, for example, is being passed to a function it must be passed as a pointer with double indirection.

Remember to deallocate any dynamic memory.

Use & appropriately in parameters. Do not pass by reference where it is not necessary. Use const where appropriate.

Run the example program TopicA.exe. Put in valid as well as invalid values. See what happens when 0 is entered. The "Press enter to end" line is part of the assignment.

Call your source code file TopicA.cpp.



Extra Credit – 10 points
For this extra credit, the total will be the correct total for the two numbers. Thus if 175 and 27 are the values entered, the result will be 202, not 3 14 5.

Remember to allocate the last array as one plus the size of the largest array. This is to allow for a possible carry. However, if values such as 22 and 65 are entered the result should be displayed as 87 not 087 or 870. The other two arrays are still exactly the size of the number of digits of the number associated with that array. Using 175 and 27, the first array in the diagram above would have 3 elements while the second array would have 2 elements. The rightmost array would have 4 elements.

You must indicate in your program status that you have done the extra credit in order to get any extra credit. Call your source code file TopicA.cpp. Do not hand in both the basic and the extra credit versions at the same time. Hand in one or the other. You can do the basic version first then hand in the extra credit version at a later time (but all by the deadline(09/10/14).



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
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
int sum(int i, int j);

int sum(int i, int j)
{
	int r = i + j;

	return r;
}

int main()
{

	int i, j;

	cout << "Please enter 2 non-negative integer values:" << endl;
	cout << " 1. ";

	int **x = new int *[];
	for (int i = 0;; ++i)
	{
		x[i] = new int[];
	}

	cin >> i;

	{
		while (i < 0)
		{
			cout << i << " is not a non-negative value. Re-enter --> 1. ";
			cin >> i;
		}
	}

	cout << " 2. ";

	int ***y = new int **;
	*y = x;
	for (int j = 0;; ++j)
	{
		y[j] = new int[];
	}

	cin >> j;

	{
		while (j < 0)
		{
			cout << j << " is not a non-negative value. Re-enter --> 2. ";
			cin >> i;
		}
	}
	cout << "\nThe values you entered and the cross sums are:" << endl;
	cout << i << endl;
	cout << j << endl;

	int total = sum(i, j);
	cout << total << endl;

	int ****z = new int ***;
	**z = new int **;
    *z = x;

	for (z = 0;; z++)
	{
		y[z] = new int[total];
	}

	cout << "\n\n Program Over" << endl;
	cout << "\n\n Press Enter to end -->" << endl;


	return total;


}


I haven't read the wall of text.

line 20, 23, 42: you need to provide the size of the array inbetween the []
i need some help please.

On what? Fails to compile, link, run or unexpected result? Error messages, etc?


1
2
3
4
5
int ****z = new int ***;
for (z = 0; ; z++) // logical error
{
	y[z] = new int[total];
}
coder777

if you see the diagram in the link at the beginning of my text, the array shouldn't be from a certain size. so should the [] stay empty? or how do i do that?
No. They have very certain size.
Your program should ask the user for 2 non-negative integer values. Be sure to verify that the values are correct. The program will then allocate the first two arrays where the size of each is the number of digits in the respective values.


The unshaded horizontal box represents an array of pointers to int.

That has size 3.

create an array in main which will hold the array sizes

Again, 3. This one is not on the diagram.

Diagram has 4 named pointers: x, y, z, q.


You have to rearrange a lot of code.
Topic archived. No new replies allowed.