Run time error in very simple matrix code

Hi,

this gives a run time error, so it doesnt print anything.. why

#include "stdafx.h"
#include "stdio.h"

void prueba (int a[])
{

a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;


}

void main(int argc, char* argv[])
{


int i=0;

int b[4];

prueba(b);

printf("%f\n", bIdea);


}



Answer this question

Run time error in very simple matrix code

  • Walter30140

    when i execute it, it shows a window with this message:

    Microsoft Visual C++ Debug Library

    Debug error!

    Program: ...what ever.exe

    run time error

    (Press Retry to debug the application)


    I press Retry and it shows:

    what ever.exe has detected a problem and must close.

  • Alessandro Camargo

    a few more Error in Code.

    First thing in your Function your Should Send The no of Element of your array also.because array bound checking didn't not take place in case of C or C++ and if you want your Following code to work so simply cast b to float and it Will print your value

    void prueba (int a[])
    {
    a[0]=1;
    a[1]=2;
    a[2]=3;
    a[3]=4;
    a[4]=5; // as you can see here Condition goes wrong.but compiler will not throw any Error Because bound checking in array Didn't take place in case of c or C++
    }

    so beter to use function in the following manner
    void prueba (int a[],int n) //where n is the no of element in your array
    {
    }
    and instead of calling your function in following manner
    prueba(b);
    call it
    prueba(b,4);

    void prueba (int a[],int n)
    {//put the necessasory Condition with n here
    a[0]=1;
    a[1]=2;
    a[2]=3;

    }

    int main(int argc, char* argv[])
    {

    int i=0;
    int b[4];
    prueba(b,4);
    printf("%f\n",(float)b[0]);
    return 0;

    }

    Thanx


  • GreenStone90

    This code looks OK assuminng that the light bulb means [ i ]. What runtime error are you seeing


  • kzu

    thanks! it was that.....

  • Donaghy

    On a second thought:

    %f format specifier is wrong, it should be %d because %d is used for int

    Try replacing %f with %d and see what happens.


  • Scott1234567

    Hmm.... it does not make any sense. I tried this program and it works just fine.

    I assume you have create a new console project, type in this code and that's all, no project properties changes or something else.

    Also, it that lightbulb supposed to be [ i ]


  • Run time error in very simple matrix code