-2

I have this code for multiplying a scalar by an n dimention vector; it compiles but crashes and I don't know what's wrong, any ideas?

 #include <stdio.h>
    int main()
    {
        int i,n,A[n]; float k,B[n];
        printf ("\n Enter vector dimension \n");
        scanf ("%d",&n);
        for (i=1; i<n+1; i++)
            { printf ("\n Enter a%d",i);
              scanf ("%d", &A[i]);
            }
        printf ("\n Enter value of scalar \n");
        scanf ("%f", &k);  
            for (i=1; i<n+1; i++)
            {B[i]=k*A[i];
            }
            for (i=1; i<n+1; i++)
            { printf ("\n B[%f]=%f",i,B[i]);
            }
        getch();
        return 0;
    }
FdHC
  • 3
  • 2
  • 1
    C-arrays start from offset 0 and not one -- do your loop as `for(i=0; i – Soren Sep 29 '14 at 23:09
  • `int i,n,A[n];` at this point, what is the value of `n`? Or is it even known? And so, what is the size of `A[n]`? Should you expect it to be big enough to hold the items you'll be inputting later when `n` has been initialized (but nothing has happened to the size of `A`)? – Paul Roub Sep 29 '14 at 23:10
  • 1
    It crashes because writing to A[n] is wrting to a memory location outside the array boundary. – Soren Sep 29 '14 at 23:11
  • By the same token, what is the size of `B`? – Paul Roub Sep 29 '14 at 23:11
  • @PaulRoub -- correct -- n is not defined at the time of the declaration of A[n] -- so that will not work either. – Soren Sep 29 '14 at 23:13
  • `A[n]` and `B[n]` declare after input `n`. – BLUEPIXY Sep 29 '14 at 23:13
  • @PaulRoub n would be the vector dimension the user inputs, B would be the resulting vector; should I initialize those in 0? I'm sorry, I'm really noob at this :( – FdHC Sep 29 '14 at 23:14
  • @Soren if I start i at 0, wouldn't that ask me for component a0 of the vector? – FdHC Sep 29 '14 at 23:21
  • Does it really compile? I doubt that. In C, all arrays must be finite during compile time thus A[n] and B[n] will not work. You should declare the actual size at compile time. Larger value won't hurt (but waste memory) smaller values will overflow if you exceed them. – sessyargc.jp Sep 29 '14 at 23:22
  • @MarieC -- that is a user interface issue -- you need to fix that in the printf by syaing `printf("...%d", i+1)` -- if you want the prompt starting at one despite the array index starting at zero – Soren Sep 29 '14 at 23:24

2 Answers2

0

In this declaration

   int i,n,A[n]; float k,B[n];

variable n is not initialized. it has an unspecified value. So the program has undefined behaviour. You have at first to enter a value for n and only after that declare arrays A[n] and B[n].

Take also into account that indices of arrays start form 0. So if you have an array with n elements then the valid range of indices will be 0, n-1

Here is how the program can look if to compile it with a compiler that supports C99.

#include <stdio.h>


int main(void) 
{
    int n;

    printf( "\n Enter vector dimension: " );
    scanf( "%d", &n );

    int a[n];
    float b[n];

    for ( int i = 0; i < n; i++ )
    { 
        printf( "Enter a%d ", i );
        scanf( "%d", &a[i] );
    }

    printf( "\n Enter value of scalar " );

    float k;
    scanf( "%f", &k );

    for ( int i = 0; i < n; i++ ) b[i] = k * a[i];

    for ( int i = 0; i < n; i++ ) printf( "\n b[%d] = %f", i, b[i] );

    //getch();

    return 0;
}   

If the input will accept the following data

3
1 2 3
0.5

then output will be

b[0] = 0.500000
b[1] = 1.000000
b[2] = 1.500000
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I initialized n at 0 and now it runs, thanks (: Now, when returning vector B=kA it gives zeroes back – FdHC Sep 29 '14 at 23:25
  • @MarieC You have at first to enter value to n and only after that to define the arrays. Arrays may not have the zero size. See my updated post. – Vlad from Moscow Sep 29 '14 at 23:34
0

The problems:

  1. The code uses the variable n in before it has been allocated a value. The first line.

    int i,n,A[n]; float k,B[n];

You might want to initialize those arrays after the first scanf call.

So you want something like this snippet.

#include <stdio.h>
#include <conio.h>  // I don't have this library.  
int main()
{   
    int n;

    printf ("\n Enter vector dimension \n");

    scanf ("%d",&n);

    int i,A[n]; 
    float k,B[n];

    for (i=1; i<n+1; i++)
        { printf ("\n Enter a%d",i);
          scanf ("%d", &A[i]);
        }

    printf ("\n Enter value of scalar \n");

    scanf ("%f", &k);  
        for (i=1; i<n+1; i++)
        {B[i]=k*A[i];
        }
        for (i=1; i<n+1; i++)
        { printf ("\n B[%f]=%f",i,B[i]);
        }

    gets();

    return 0;
} 
tomilay
  • 685
  • 6
  • 11