Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is wrong?

0 views
Skip to first unread message

prampuria

unread,
Oct 1, 2004, 12:40:10 PM10/1/04
to
//My 2-dimensional array is like this.

int array[7][4];

1 3 6 10 14 18 22
2 5 9 13 17 21 25
4 8 12 16 20 24 27
7 11 15 19 23 26 28


//I need to run through it "diagonally" like this and print the data:

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

//my current code works fine right now. However. i have a concern i.e.
when I replace two lines in the second for loop with the commented
code, my program fails to give correct ouput. infact it breaks.
//Although both the logic are same, i like the one thats commented
because thats the one that came to my mind first and also in my
opinion a better design for the for loop. Please explain why the
commented code fails. I don't see the problem.

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iomanip>

using std::vector;

using std::cout; using std::endl;
int main(){
vector<vector<int> >VectorExpression;
vector<int> a;
vector<int>::iterator sit;
a.push_back(1);
a.push_back(3);
a.push_back(6);
a.push_back(10);
a.push_back(14);
a.push_back(18);
a.push_back(22);


vector<int> b;
b.push_back(2);
b.push_back(5);
b.push_back(9);
b.push_back(13);
b.push_back(17);
b.push_back(21);
b.push_back(25);


vector<int> c;
c.push_back(4);
c.push_back(8);
c.push_back(12);
c.push_back(16);
c.push_back(20);
c.push_back(24);
c.push_back(27);

vector<int> d;
d.push_back(7);
d.push_back(11);
d.push_back(15);
d.push_back(19);
d.push_back(23);
d.push_back(26);
d.push_back(28);


VectorExpression.push_back(a);
VectorExpression.push_back(b);
VectorExpression.push_back(c);
VectorExpression.push_back(d);


int anInt;
vector<vector<int> >::iterator it;
vector<int> MyList;
for (it = VectorExpression.begin(); it != VectorExpression.end();
++it)
{
MyList = *it;
for (sit = MyList.begin(); sit != MyList.end(); ++sit)
{
cout << std::setw(4) << *sit << ' ';
}
std::cout << std::endl;

}


cout << endl << endl;

typedef vector<vector <int> >::size_type vec_sz;
vec_sz rows = VectorExpression.size();


typedef std::vector<int>::size_type size;
size cols = b.size();

//ith row jth column


for(size i =0 ; i!=rows; i++)
for(size j=0; j!=cols; j++)
if(i >= j)
printf("%4d", VectorExpression[i-j][j]);
else if(i < j) {
printf("\n");
break;
}

for(vec_sz j = 0; j!= cols; j++){
for(size i = rows, index=1; i>0; i--, index++)
//This line should be for(size i = rows-1, index=1; i>=0; i--,
index++)
if((j+index) < cols)
printf("%4d", VectorExpression[i-1][j+index]);
//The line should be printf("%4d", VectorExpression[i][j+index])
printf("\n");
}

system("pause");
return 0;

}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Carl Barron

unread,
Oct 2, 2004, 6:05:11 AM10/2/04
to
prampuria <pram...@yahoo.com> wrote:

> typedef vector<vector <int> >::size_type vec_sz;

is this [most likely] std::size_t? If so it is probably unsigned and
thats the problem. since for this problem it is most likely that the
results of vector<...>::size() will fit in a long since the total data
for your vector<vector<...> > is greater than than
vector<...>::size() *vector<vector<...> >::size() and
the product must not overflow or the process fails

typedef long vec_sz;
should work just fine. It did with my templates of a size known at
compile time Matrix class.

M Jared Finder

unread,
Oct 2, 2004, 10:40:21 PM10/2/04
to
prampuria wrote:

<snip>


> typedef std::vector<int>::size_type size;
> size cols = b.size();
>
>
> //ith row jth column
>
>
> for(size i =0 ; i!=rows; i++)
> for(size j=0; j!=cols; j++)
> if(i >= j)
> printf("%4d", VectorExpression[i-j][j]);
> else if(i < j) {
> printf("\n");
> break;
> }
>
> for(vec_sz j = 0; j!= cols; j++){
> for(size i = rows, index=1; i>0; i--, index++)
> //This line should be for(size i = rows-1, index=1; i>=0; i--, index++)

What will the value of i be if rows is 0? Remember that size is of an
unsigned type.

> if((j+index) < cols)
> printf("%4d", VectorExpression[i-1][j+index]);
> //The line should be printf("%4d", VectorExpression[i][j+index])
> printf("\n");
> }
>
> system("pause");
> return 0;
>
> }

-- MJF

0 new messages