Code:
/*randomPointers.c
* this program assigns a 2d array with random numbers and returns the largest
* and smallest element in the array. It also takes use of the ternary operator.
* This program under TrueSoftware(TM) liscence. Feel free to do whatever you want
* with it.
* authour: Caroundw5h 8/21/2004
*
*/
/* this can be improeved with pointers somhow, i'm sure. and also having one function
that tells if the number is High or Low. I know it. too tired right now. */
#include <stdio.h>
#include <stdlib.h> //for random
#include <time.h>//for random
#define LEN 10
void InitArray(int (*)[LEN], int);//Initializes the array
void Show(int (*)[LEN], int);/* shows a multidemsional array */
int Max(int (*)[LEN], int);/* returns the Highest num in a 2d array */
int Min(int (*)[LEN], int); /* returns Lowest num in given array */
int IndiceFinder(int [][LEN],int,int);
int main(void){
srand(time(NULL));// SEED RANDOM GENERATOR WITH TIME ONCE
int array[LEN][LEN]; // 10 X 10 array
int Indice, High, Low;
InitArray(array, LEN);
Show(array, LEN);//notice when called called with just name nothing else
//whether single or multi
putchar('\n');
High = Max(array,LEN);
printf("Largest integer in array is %d\n", High );
IndiceFinder(array,LEN,High);
Low = Min(array, LEN);
printf("Smallest integer in array is %d\n", Low);
//IndiceFinder(array,LEN,High);
getchar();
return 0;
}
void Show(int (*array)[LEN], int rows){
printf("This array contains:\n\n");
/* notice the second parameter isn't necessary really but...*/
int cnt, cnt2;
for(cnt = 0;cnt < rows; cnt+=1){
printf("Row %d: ", cnt);
for(cnt2=0;cnt2 < LEN; cnt2++){
printf(" %3d", *(*(array + cnt) + cnt2));
}
printf("\n");
}
return;
}
int Max(int (*seq)[LEN], int rows){
/* just like int ptr decalration *seq is a pointer to a const int of LEN values. if was
single ptr would declare (*ptr) and now right followups, I see the pattern */
int i,j, highest = seq[0][0];// highest starts off with the first number in array
for(i = 0; i < rows; i+=1){
for(j=0; j < LEN; j++){
highest = highest < seq[i][j] ? seq[i][j] : highest;
/* is seq[0][0] < seq[0][0] if yes then highest = seq[0][0]
if seq[0][1] < seq[0][0] then higest = seq[0][1]. */
}
}
return highest;
}
int Min(int (*seq)[LEN], int rows){
/* just like int ptr decalration *seq is a pointer to a const int of LEN values. if was
single ptr would declare (*ptr) and now right followups, I see the pattern */
int i,j, lowest = seq[0][0];// highest starts off with the first number in array
for(i = 0; i < rows; i+=1){
for(j=0; j < LEN; j++){
lowest = lowest > seq[i][j] ? seq[i][j] : lowest;
}
}
return lowest;
}
void InitArray(int (*array)[LEN], int rows){
int i,j,RandNum;
for(i = 0; i < rows; i++){
for(j = 0; j<LEN; j++){
RandNum = (rand() % 100) + 1;// a random number each time it goes through the loop
*(*(array + i) + j) = RandNum;
//array[i][j] = RandNum;
}
}
return;
}
IndiceFinder(int (*array)[LEN],int rows, int num){
/* logic: loop through a given 2d array and search for a num. If that num is found
then note the indces. That is its row and clm */
int i,j,HighCount =0;
for(i = 0; i< rows; i++){
for(j=0; j< LEN; j++){
if ( ( array[i][j] == num) ){
HighCount+=1;
printf("the %s is in row %d clm %d\n\n", (HighCount == 1 ?"value" : "other")
,i,j);
}
}
}
return ;
}