Hello, not long ago I started studied C and now I'm having some problems with developing a small program, that would search each row of my matrix, pick the highest element and switch it with the element on diagonal. It was no problem to do it by passing the array to a function, however the it's hard for me to understand the correct way of doing the same with pointers. I got the "Suspicious pointer conversion" error, and various others at the function defining stage. Here's my code, hope you could help me with at least defining the function:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define SIZE 4
int vert_apstr(int array[][SIZE], int, int);
int adr_apstr(int *arr, int, int); /* don't know how correct is this */
void main(){
int matrix[SIZE][SIZE], matrix2[SIZE][SIZE];
int i,j;
clrscr();
randomize();
printf("\n 1) Function (value) \n");
printf("\n Old Matrix\n\n");
for (i=0;i<SIZE;i++){
for (j=0;j<SIZE;j++){
matrix[i][j]=random(10);
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
vert_apstr(matrix,SIZE,SIZE);
gotoxy(31,5);
printf("New matrix\n\n");
for (i=0;i<SIZE;i++){
gotoxy(30,i+7);
for (j=0;j<SIZE;j++){
printf("%3d ", matrix[i][j]);
}
printf("\n\n");
}
printf("\n 2) Function (adress)\n");
/* something similar, but with pointers */
getch();
}
int vert_apstr(int array[][SIZE], int aug_r1, int aug_r2){
int i,j,buf;
int hi_elem, hi_col;
for (i=0; i<aug_r1; i++){
hi_elem=array[i][0];
hi_col=0;
for (j=0; j<aug_r2; j++){
if (array[i][j]>hi_elem){
hi_elem=array[i][j];
hi_col=j;
}
}
buf=array[i][i];
array[i][i]=array[i][hi_col];
array[i][hi_col]=buf;
}
return array[aug_r1][aug_r2];
}
The things I want to know are:
1)
Code:
int adr_apstr(int *arr, int, int);
- prototype
Code:
int adr_apstr(int *arr, int aug_r1, int aug_r2){ ... }
- the function itself
Code:
adr_apstr(matrix2,SIZE,SIZE);
- call for a function
What is the correct way, to write these 3 things?
2) if I typed elements myself, I would have done something like this "scanf("%d",matrix2+i)", but if I use randomize(), how can I give the array a value? I tried "for (i=0;i<SIZE*SIZE;i++) (matrix2+i)=random(10);", but it just gave me another error. Glad, if you could help!