After a lot of research I found out some things that I would like to verify and use in my homework. When passing a 2D array to a function as pointer, it is treated as a 1D, so hypothetically on the same line, is that right to say?
So if I want to find the address of a cell (int) I just add +4n?
And now for my homework:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
usingnamespace std;
constint AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
printing(Array);
system("PAUSE");
return 0;
}
void FillingRandomly(int *ArrayPtr)
{for(int i=0;i<AS;i++)
{for (int j=0;j<AS;j++)
{
*(*(ArrayPtr +i)+j)=rand()%56+2;
}
}
}
void printing(int *Array)
{
for(int i=0;i<AS;i++)
{for (int j=0;j<AS*AS;j++)
{int counter = 0;
cout<<((Array[i] +j))<<setw(5);
if ((Array[i] +j)%AS == 0)
cout << endl << endl;
}
}
}
void forsorting(int *Brray, int funny)
{
int dice = 0;
int super = 0;
int space=0;
//Sorting Array[][] which is treated like Array[]
{
for (int pass = 0; pass < AS - 1; pass++) {
for (int k = 0; k < AS - 1; k++) {
int temp;
if(*(Brray+k)==*(Brray+k+1))
{
temp=*(Brray+k);
*(Brray+k)=*(Brray+k+1);
*(Brray+k+1)=temp;
}
}
}
}
}
I have to create a program which has 3 functions, one to create and fill an array at random, one to print it on the screen, and one to sort it. Im only having about 2 errors that I cant seem to know how to fix Error 2 error C2100: illegal indirection c:\users\pc\desktop\usb\anthony\documents\visual studio 2012\projects\finalll\finalll\finalll.cpp 55 and related to it Error 3 error C2106: '=' : left operand must be l-value c:\users\pc\desktop\usb\anthony\documents\visual studio 2012\projects\finalll\finalll\finalll.cpp 55 and 4 IntelliSense: operand of '*' must be a pointer c:\Users\pc\Desktop\USB\Anthony\Documents\Visual Studio 2012\Projects\Finalll\Finalll\Finalll.cpp 55
.
Im pretty sure that the sorting technique is correct as its the same as the one in my course, but Im not too sure about the usage of the pointers.
Sorry if my english is not too good, it's not my main language
Thank you
void FillingRandomly(int (*)[AS]); // lines 11-12... these are correct
void printing(int (*)[AS]);
void FillingRandomly(int *ArrayPtr) // line 51 .. this doesn't match -- it's wrong
void printing(int *Array) // line 60 .. also doesn't match
// These are your prototypes. They are correct
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
// These are your definitions, they are wrong
void FillingRandomly(int *ArrayPtr)
void printing(int *Array)
// change them to match your prototypes:
void FillingRandomly( int (*ArrayPtr)[AS] )
void printing( int (*Array)[AS] )