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.

10
  • @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. Commented Sep 12, 2014 at 21:15
  • @user3121023 It is C++ feature not C. Maybe C99 added that feature but then you have compile with c99 flag. Commented Sep 12, 2014 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! Commented Sep 12, 2014 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. Commented Sep 12, 2014 at 21:24
  • How do you know it's working before you compile it? Commented Sep 12, 2014 at 21:28

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

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.
Yes, that will limited to size 10. Check here how to allocate dynamic memory programiz.com/c-programming/c-dynamic-memory-allocation
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, Check the answer now. I guess you were trying do something like that.
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.