Runtime Error

Well, I accidentally delete when Editing... I make a little change in the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>

int main(){
  unsigned int x, y;
  const unsigned int z=2147483648;
  char array[64]={0};
  char a, b;
  int i, j;
  int m;
  
  scanf("%d %d", &j, &i);
  scanf(" %c %c", &a, &b);
  scanf("%u %u", &x, &y);
  
  for(m=0; m<32; m++){
    if( (x&z) == z){
      array[m]=a;
    }
    else{
      array[m]=b;
    }
    x = x<<1;
  }
  
  for(m=0; m<(i*j); m++){
    if( (y&z) == z){
      array[m+32]=a;
    }
    else{
      array[m+32]=b;
    }
    y = y<<1;
  }
  
  for(m=0;m<(i*j);m++){
    if(m%j==0)
      printf("\n");
    printf("%c", array[m]);
  }
}
Last edited on
Hi,

This is what I get when compiling, using the gear icon top right of your code, with all the warnings turned on.

Make sure you compile with the highest level of warnings.

3:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wpedantic] 
In function 'int main()': 8:13: error: uninitialized const 'i' [-fpermissive] 
8:16: error: uninitialized const 'j' [-fpermissive] 
11:24: warning: writing into constant object (argument 2) [-Wformat=] 
11:24: warning: writing into constant object (argument 3) [-Wformat=] 
13:24: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'unsigned int*' [-Wformat=] 
13:24: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'unsigned int*' [-Wformat=] 
9:10: warning: unused variable 'n' [-Wunused-variable]


Some things that help avoiding these:

Declare and initialise 1 variable per line;
When using scanf, make use of it's return value - to see that it worked.
Avoid magic numbers like 36 or 32 in your code, make them a const variable instead, and use that variable name.

Good Luck !!
Last edited on
Apart from what TheIdeasMan stated:

On line 27/30: if (i*j) > 4 the index will go out of bounds. 8 * 7 + 32 = 88 > 36
How about this:

1
2
3
scanf("%d %d", &j, &i);
  int size = i*j;
  char array[size]={0};


I get warnings:

[Warning] excess elements in array initializer [enabled by default]
[Warning] (near initialization for 'array') [enabled by default]


But I get no warning if I don't initialize it.
Last edited on
The compiler cannot initialize the array because it is dynamic.

To set it to 0 you may use memset:

http://www.cplusplus.com/reference/cstring/memset/?kw=memset

Or fill:

http://www.cplusplus.com/reference/algorithm/fill/?kw=fill


Due to the +32 it is still out of bounds.
you cannot initialise VLAs


> format '%d' expects argument of type 'int*', but argument 2 has type 'unsigned int*'
you shouldn't lie.
%u is for unsigned


> The compiler cannot initialize the array because it is dynamic.
calloc() and new allow you initialisation of dynamic arrays.
Last edited on
Topic archived. No new replies allowed.