Solved

C++ jagged array (array of pointers)

Posted on 2013-11-10
4
1,601 Views
Last Modified: 2013-11-10
I am attempting to create a jagged array in C++, which I believe is created using an array of pointers.  The goal is to create a jagged array to represent the 7 days of the week and expenditures for the day.  For example, if someone spends:

Sunday - $7, $17, $23
Monday - 5, 10, 6, 8, 25
Tuesday - 13
Wednesday - 2, 7, 9, 11, 31, 7, 4
Thursday - 5, 6, 2, 18
Friday - 0
Saturday - 10, 22, 8

My C++ is a bit rusty, especially in the area of pointers.

I know that an array of 7 pointers (representing the 7 days of the week) can be written with

 int *weekArray[7] 

Open in new window


however I am unsure on how to implement those 7 pointers to be the integers I require.
0
Comment
Question by:Autkast
  • 3
4 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39637696
You have implemented the pointers properly.
Now you need to create new arrays for each of the days, like this:
weekArray[0] = new int[3]; // expenditures for Sunday
weekArray[1] = new int[5]; // expenditures for Monday
etc.

Open in new window

To access the values you will use the following code:
weekArray[0][0] = 7; weekArray[0][1] = 17; weekArray[0][2] = 23; // Sunday
weekArray[1][0] = 5; weekArray[1][1] = 10; etc. // Monday

Open in new window

Do not forget to delete the pointers after you have finished with them:
delete [] weekArray[0];
delete [] weekArray[1];
etc.

Open in new window

However...
You have tagged the question as a C++ question which automatically qualifies to a logical cross question: why pointers? When you deal with c++ you would use C++ features. First think that comes to mind is std::vector
the code will be as simple as this with the vector:
std::vector<int> weekArray[7];
weekArray[0][0] = 7; weekArray[0][1] = 17; weekArray[0][2] = 23; // Sunday
weekArray[1][0] = 5; weekArray[1][1] = 10; etc. // Monday

Open in new window

Note, that in this case you do not need to manage memory with new/delete
0
 

Author Comment

by:Autkast
ID: 39637763
Ok, I like the vector idea, however I as I said, I am a bit rusty in C++.  When I declare the vector as you have shown,
	
        std::vector<int> weekArray[7];
	//initialize weekArray
	// sunday
	weekArray[0][0] = 7; 
	weekArray[0][1] = 17; 
	weekArray[0][2] = 23;
	// monday
	weekArray[1][0] = 5;
	weekArray[1][1] = 10; 
	weekArray[1][2] = 6; 
	weekArray[1][3] = 8; 
	weekArray[1][4] = 25;
	// etc...

Open in new window


I get that the vector subscript is out of range.

For a 2 dimensional vector, would it be declared as?:
	std::vector<vector<int>> weekArray;

Open in new window


I also get a subscript out of range on this run as well.  I am not sure how to initialize a vector obviously.  If I do create the vector properly, are the same for-loops used when adding or averaging the elements in the vector as when adding/averaging multidimensional arrays?
0
 
LVL 24

Expert Comment

by:chaau
ID: 39637769
you insert elements to a vector using one of the following methods:
insert()
push_back()
I recommend you use the latter as it is easier.
Check this for reference.
0
 
LVL 24

Accepted Solution

by:
chaau earned 100 total points
ID: 39637781
So, to convert it to your case the syntax will be:
std::vector<int> weekArray[7];
weekArray[0].push_back(7); weekArray[0].push_back(17); weekArray[0].push_back(23); // Sunday
weekArray[1].push_back(5); weekArray[1].push_back(10); etc. // Monday

Open in new window

When you have inserted the values you could use operator[].
BTW, you are right about std::vector<vector<int>> weekArray;. However, I thought that because the number of days in a week is a constant value you could just use an array of vectors
You may consider creating a class for the weekDay, where the vector of expenditures is a member. Then you would just use a fixed number of these classes. By doing this you'd simplify the access to the elements of your weekDay class.
Consider this:
class WeekDay 
{
public:
std::vector<int> expenditures;
}
enum
{
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
}
WeekDay week[7];
void main()
{
week[Mon].expenditures.push_back(1);
week[Sat].expenditures.push_back(2);
}

Open in new window

If you need to add another property of the weekDay, you would just add another member inside the WeekDay class:
class WeekDay 
{
public:
std::vector<int> expenditures;
bool iWeekEnd;
}
void main()
{
week[Mon].isWeekend=false;
week[Sat].isWeekend=true;
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 104
C++ error cannot convert from std::string to const char * 6 128
Problem with SqlConnection 4 159
Add values of each row in an array 3 50
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now