i think the logic in my program is alright but the only poblem im having is saving the values throughout the process and outputting it at once. Its supposed to be a string that is constantly updated, in this case called rule. it keeps giving me a segmentationaly fault for some reaosn and ive not been able to figureout why.
any help and alternative methods are appreciated!
the segmentationaly error occours once it reads the 1st r_o value in the second for loop.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
int main(int argc, char **argv)
{
int width = 0;
int height = 0;
double r_o = 0;
double g_o = 0;
double b_o = 0;
double r_e = 255;
double g_e = 0;
double b_e = 0;
int r_number = 0;
int g_number = 0;
int b_number = 0;
int counter = 0;
int Rule =0;
int rule[Rule];
int p_count = 0;
scanf("%d", &width);
scanf("%d", &height);
int i = 0;
int j = 0;
for (i = 0; i<= width; i++){
rule[Rule]='[';
Rule++;
for (j = 0; j<=height; j++) {
scanf("%lf", &r_o);
counter = 0;
if (r_o < r_e) {
r_number = r_e - r_o;
while(counter<=r_number) {
rule[Rule]='R';
Rule++;
counter++;
}
} else if (r_o > r_e) {
r_number = r_o - r_e;
while (counter <= r_number) {
rule[Rule]='r';
Rule++;
counter++;
}
}
scanf("%lf", &g_o);
counter = 0;
if (g_o < g_e) {
g_number = g_e - g_o;
while (counter <= g_number) {
rule[Rule]='G';
Rule++;
counter++;
}
} else if (g_o > g_e) {
g_number = g_o - g_e;
while (counter <= g_number) {
rule[Rule]='g';
Rule++;
counter++;
}
}
scanf("%lf", &b_o);
counter =0;
if (b_o < b_e) {
b_number = b_e - b_o;
while (counter <= b_number) {
rule[Rule]='B';
Rule++;
counter++;
}
} else if (b_o > b_e) {
b_number = b_o - b_e;
while (counter <= b_number) {
rule[Rule]='b';
Rule++;
counter++;
}
}
rule[Rule]='F';
Rule++;
}
rule[Rule]=']';
Rule++;
rule[Rule]='+';
Rule++;
rule[Rule]='f';
Rule++;
rule[Rule]='-';
Rule++;
}
for(p_count = 0; p_count <=Rule;p_count++) {
printf("%c",rule[p_counter]);
}
return(0);
}
dont be harsh..im fairly new to this stuff lol