Help with two dimensional arrays
May 2, 2014 at 10:35am UTC
I'm not sure if I'm reading too much into it. But I'm trying to make a 5x5 two dimensional array count as such.
1 2 3 4 5
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
without using
1 2
ar[5][5] = {1, 2, 3, 4, 5}{6, 7, 8, 9, 10}etc...
This is how I'm trying to write my code...
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
#include <iostream>
using namespace std;
void fill(int ar[][5], int size);
void print(int ar[][5], int size);
int main()
{
int ar[5][5];
fill(ar, 5);
print(ar, 5);
return 0;
}
void fill(int ar[][5], int size)
{
for (int row = 1; row < size; row++)
{
for (int col = 0; col < size; col++)
{
ar[row][col] = row + col;
}
}
}
void print(int ar[][5], int size)
{
for (int row = 1; row < size; row++)
{
for (int col = 0; col < size; col++)
{
cout << ar[row][col] << '\t' ;
}
cout << endl;
}
}
Am I over thinking it? Please someone help. Thank you also
May 2, 2014 at 10:39am UTC
No...Its good you are learning more...Keep on
May 2, 2014 at 10:48am UTC
Ok... So, I was thinking that I can increment the for loops differently. But, whenever I do, it won't output the 5x5 two dimensional array anymore. It outputs something like
1 2 3 4 5
1 4
2 5
3 6
4 7
5 8
Why is it doing that?
May 2, 2014 at 11:09am UTC
Not gonna lie... I've been stuck on this for four hours. I'm probably just talking to myself. Haha. But, here it is. I finally got it.
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
#include <iostream>
using namespace std;
void fill(int ar[][5], int size);
void print(int ar[][5], int size);
int main()
{
int ar[5][5];
fill(ar, 5);
print(ar, 5);
return 0;
}
void fill(int ar[][5], int size)
{
for (int i = 1; i < 20; i += 5)
{
for (int j = 0; j < size; j++)
{
ar[i][j] = i + j;
}
}
}
void print(int ar[][5], int size)
{
for (int i = 1; i < 20; i += 5)
{
for (int j = 0; j < size; j++)
{
cout << ar[i][j] << '\t' ;
}
cout << endl;
}
}
Thanks alfredokang for the encouragement.
May 2, 2014 at 11:32am UTC
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
#include <iostream>
#include <iomanip>
#include <numeric>
#include <iterator>
const int COLS = 5 ;
void fill( int ar[][COLS], int size )
{
int n = 0 ;
for ( int i = 0 ; i < size ; ++i )
for ( int j = 0 ; j < COLS ; ++j ) ar[i][j] = ++n ;
}
void print( const int ar[][COLS], int size )
{
for ( int i = 0 ; i < size ; ++i )
{
for ( int j = 0 ; j < COLS ; ++j )
std::cout << std::setw(3) << ar[i][j] << ' ' ;
std::cout << '\n' ;
}
}
int main()
{
{
int a[5][COLS] ;
fill( a, 5 ) ;
print( a, 5 ) ;
}
std::cout << "-------\n" ;
{
int b[5][COLS] ;
int n = 0 ;
for ( auto & row : b ) for ( int & i : row ) i = ++n ;
for ( const auto & row : b )
{
for ( int i : row ) std::cout << std::setw(3) << i << ' ' ;
std::cout << '\n' ;
}
}
std::cout << "-------\n" ;
{
int c[5][COLS] ;
std::iota( std::begin( c[0] ), std::end( c[COLS-1] ), 1 ) ;
for ( const auto & row : c )
{
for ( int i : row ) std::cout << std::setw(3) << i << ' ' ;
std::cout << '\n' ;
}
}
std::cout << "-------\n" ;
}
http://coliru.stacked-crooked.com/a/17c9486bc8b54df3
May 2, 2014 at 11:35am UTC
hope the following code helps....its from a text file if you infile....only works for square matrixs
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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
using namespace std;
void matrix(vector<int >&data)
{
int arr1[10][10];
int result[10][10];
ifstream infile("input.txt" );
int d=0;
//////////////////////////////infiling
while (infile>>d)
{
data.push_back(d);
}
int counter=0;
int asize=sqrt(data.size());
///////////////////////////////////storing results in 2d array;
for (int i=0;i<asize;i++)
{
for (int j=0;j<asize;j++)
{
arr1[i][j]=data.at(counter);
counter++;
}
}
////////////////////////////////////diaplaying
for (int i=0;i<asize;i++)
{
for (int j=0;j<asize;j++)
{
cout<<arr1[i][j]<<" " ;
}
cout<<endl;
}
}
int main()
{
vector<int >data;
matrix(data);
return 0;
}
Topic archived. No new replies allowed.