1

I made a code in CodeBlocks that seems perfectly fine, but when I compile it the following (in the images in the link) happens: http://postimg.org/gallery/imbtu6ns/ Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int x,num[x],n,small,big;
    printf("Type how many numbers you will use: ");
    scanf("%d",&n);
    for (x=0;x<n;x++){
        printf("Type a number: ");
        scanf("%d",&num[x]);
    }
    big=num[0];
    small=num[0];
    for (x=0;x<n;x++){
        if (num[x]<small){
            menor=num[x];
        }
        if (num[x]>big){
            maior=num[x];
        }
    }
    printf("Maior: %d\n",big);
    printf("Menor: %d\n",small);
    system("pause");
    return 0;
}

Its a code to read n numbers from the user and then print the smaller and the bigger number. I think there is no problem in the code. What is this error? I tryed doing whats is written here: Windows popup: X.exe has stopped working (Code::Blocks), but it didnt work.

Community
  • 1
  • 1
Cepphei
  • 19
  • 6
  • @user3121023 Thanks, this was a really simple solution, it looks like its working now, will execute it sometimes and check if it really was the perfect solution. – Cepphei Sep 12 '14 at 21:15
  • @user3121023 It is C++ feature not C. Maybe C99 added that feature but then you have compile with c99 flag. – Md. Al-Amin Sep 12 '14 at 21:21
  • @user3121023 Yes this really was a perfect yet simple solution, thanks a lot! I would mark as THE answer, but you posted it as a comment! – Cepphei Sep 12 '14 at 21:23
  • @Md.Al-Amin Its working perfectly fine using CodeBlocks and compiling as a C file. No idea about what is C99 or a c99 flag. – Cepphei Sep 12 '14 at 21:24
  • How do you know it's working before you compile it? – Daniel Kamil Kozar Sep 12 '14 at 21:28
  • @DanielKamilKozar Well the code was looking perfectly fine, I assumed the error was not about the logic, but rather about some other thing, and I was right. Well to be honest, there was some faulty logic in it. – Cepphei Sep 12 '14 at 21:31
  • What are the problems you are having? – jww Sep 12 '14 at 22:28
  • @jww Its all solved, you can see what it was by reading the post and looking at the screen shots in the link. The solution is first coment, made by user3121023. – Cepphei Sep 13 '14 at 00:19
  • @Cepphei - I read the question. You never stated what problem you are having.. – jww Sep 13 '14 at 00:20
  • regarding this line: int x,num[x],n,small,big; The variable 'x' value is not defined/trash/whatever is on the stack when trying to define the array 'num[x]' – user3629249 Sep 13 '14 at 14:50

1 Answers1

1

Use a fixed number when declaring an array or use memory allocation technique.

Change this line:

int x,num[x],n,small,big;

To

int x,num[10],n,small,big;

Here is code with memory allocation technique. I guess you are trying do something like that.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int x,*num,n,small,big;
    printf("Type how many numbers you will use: ");
    scanf("%d",&n);
    num = (int*)malloc(n*sizeof(int));
    for (x=0;x<n;x++){
        printf("Type a number: ");
        scanf("%d",num+x);
    }
    big=*(num+0);
    small=*(num+0);
    for (x=0;x<n;x++){
        if (*(num+x)<small){
            small=*(num+x);
        }
        if (*(num+x)>big){
            big=*(num+x);
        }
    }
    free(num);
    printf("Maior: %d\n",big);
    printf("Menor: %d\n",small);
    return 0;
}
Md. Al-Amin
  • 1,423
  • 1
  • 13
  • 26
  • Please help me understand, creating the vector var as num[10] will not limitate the vector to the size of 10? Thanks for the answer. – Cepphei Sep 12 '14 at 21:04
  • Yes, that will limited to size 10. Check here how to allocate dynamic memory http://www.programiz.com/c-programming/c-dynamic-memory-allocation – Md. Al-Amin Sep 12 '14 at 21:07
  • x is initialized in the for command, but even if I initialize it in the var declaration, I still get an error, not in the moment I compile, but in a certain time after I've executed the code. – Cepphei Sep 12 '14 at 21:12
  • @Cepphei, Check the answer now. I guess you were trying do something like that. – Md. Al-Amin Sep 12 '14 at 21:19
  • @Md.Al-Amin Thanks again, it was really instructive, but the best solution was this: "You might move the declaration of num[] to the line after scanf("%d",&n); as int num[n]; Then num will have n elements." from user3121023 – Cepphei Sep 12 '14 at 21:22
  • You forgot to `free` the memory, thus creating a memory leak. – Daniel Kamil Kozar Sep 12 '14 at 21:30