Skip to content

Instantly share code, notes, and snippets.

@muhasturk
Created July 19, 2015 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhasturk/093e68e269eee087eaa5 to your computer and use it in GitHub Desktop.
Save muhasturk/093e68e269eee087eaa5 to your computer and use it in GitHub Desktop.
Find Highest Value and Its Indexes
//
// main.c
// Gokay -> Find Highest Value and Indexes in Multidimensional Array
//
// Created by Mustafa Hastürk on 19/07/15.
// Copyright (c) 2015 Mustafa Hastürk. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 8
#define COLUMN 8
#define MAX_VAL 50
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
void setCollection(int *);
void printCollection(int *, int);
int main(int argc, const char * argv[]) {
int *collection = (int*) malloc((ROW*COLUMN) * sizeof(int));
srand((unsigned)time(NULL));
setCollection(collection);
int locations [ROW*COLUMN][2] = {0}; // need more wise allocation
int highestValue = *(collection);
int sameCount = 0;
for (int c_row = 0; c_row < ROW; c_row++) {
for (int c_column = 0; c_column < COLUMN; c_column++) {
int val = *(collection + (c_row * COLUMN) + c_column);
if (val > highestValue) {
highestValue = val;
sameCount = 1;
locations [sameCount-1][0] = c_row;
locations [sameCount-1][1] = c_column;
}
else if (val == highestValue) {
locations [sameCount][0] = c_row;
locations [sameCount][1] = c_column;
sameCount++;
}
}
}
printCollection(collection, highestValue);
printf("En yuksek olan" ANSI_COLOR_RED " %d " ANSI_COLOR_RESET "degeri:\n" , highestValue);
for (int n = 0; n < sameCount; n++) {
printf("[%d] [%d] \n", locations[n][0], locations[n][1]);
}
return 0;
}
void setCollection(int *collection) {
for (int c_row=0; c_row < ROW; c_row++) {
for (int c_column=0; c_column < COLUMN; c_column++) {
*(collection + (c_row * COLUMN) + c_column) = rand() % MAX_VAL;
}
}
}
void printCollection(int *collection, int highestValue) {
for (int c_row=0; c_row < ROW; c_row++) {
for (int c_column=0; c_column < COLUMN; c_column++) {
int currValue = *(collection + (c_row * COLUMN) + c_column);
if (highestValue == currValue ) {
printf(ANSI_COLOR_GREEN "%2d " ANSI_COLOR_RESET, currValue);
}
else {
printf("%2d ", currValue);
}
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment