Skip to content

Instantly share code, notes, and snippets.

@vinnietech
Created March 7, 2020 18:15
Show Gist options
  • Save vinnietech/e83f6e51d9bf0059db0a557cd64593e9 to your computer and use it in GitHub Desktop.
Save vinnietech/e83f6e51d9bf0059db0a557cd64593e9 to your computer and use it in GitHub Desktop.
Opdracht V in C
#include <stdio.h>
int calc_hor_len(char *map) {
int index=0;
int start_counting = 0; // if 1 then TRUE, if 0 then FALSE
int counter=0;
while (map[index])
{
if (map[index] == '\n') {
if (start_counting == 1) {
return counter;
} else {
start_counting = 1;
}
}
if (start_counting == 1 && map[index] != '\n') {
counter++;
}
index++;
}
return(index);
}
int cal_vert_len(char *map)
{
int counter =0;
int index=0;
while(map[index]) {
if (map[index] == '\n') {
counter++;
}
index++;
}
return counter - 1;
}
int main(void) {
// read the map from stdi and save in the variable map
int c;
int i = 0;
char map[10000];
while ((c=getchar()) != EOF) {
map[i] = c;
i++;
}
// get the length of the horizontal line of the map
int horizontal_lgt = calc_hor_len(map);
int vertical_lgt = cal_vert_len(map);
printf("%s", map);
printf("%d", horizontal_lgt);
printf("%d", vertical_lgt);
char mda_map[vertical_lgt][horizontal_lgt];
int row_count = 0;
int cell_count = 0;
int index=0;
while (map[index]) {
if (map[index] == '\n') {
row_count++;
cell_count=0;
} else {
if (row_count > 0) {
mda_map[row_count-1][cell_count] = map[index];
}
}
cell_count++;
index++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment