Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return an array with all integers from a to b

Tags:

arrays

c

The exercise says "Create a function with two parameters a and b which are integers and the function will return an array of integers with every number from a to b.

#include <stdio.h>
#include <stdlib.h>

void exc(int a, int b){
  int i,k=0,d[k];
  for(i=a;i<=b;i++){
  d[k]=i;
  k++;
  printf("%d ",d[k]);
  }
}

int main(void){
 int c,d;
 printf("Give first integer: ");
 scanf("%d",&c);
 printf("Give second integer: ");
 scanf("%d",&d);
 exc(c,d);
 system("pause");
}

The problem is that if I put for example c=2 and d=5 the program returns something like 2088806975 16384 1 2293536 instead of 2 3 4 5. Where is the problem? Thanks

like image 950
captain Avatar asked Apr 11 '11 21:04

captain


2 Answers

For starters

If your main() has return type int, don't forget to return a value from it!

int main(void)
{
   /* code here */

   return 0;
}

Problem 1

By

d[k]=i;
k++;
printf("%d ", d[k]);

I think you meant

d[k]=i;
printf("%d ", d[k]);
k++;

otherwise you're printing the "next" array element each time, which will be one-past-the-end of the array on the last loop iteration.

Problem 2

int i,k=0,d[k];

You make an array d of size k where k is 0. I think you intended for the array to automatically resize when you write k++, but this is not the case. The array is created with zero elements, and then that's its size for all time.

Your next instinct may be to create the array big enough in the first place:

int d[b-a+1];

Unfortunately, this is most likely wrong, too. It relies on a feature called Variable Length Arrays (or "VLAs"); although a GCC compiler extension (and, incidentally, C99) does allow this (and it's not clear whether you have that extension enabled and/or are allowed to use it in your homework — I will assume for this answer that you do not / are not), the language itself does not allow an array with a dynamic size.

What do I mean by dynamic size?

I mean that the variables a and b depend on user input: they are not known at compile-time. In general, the size of an array must be known at compile-time.

Note: If you use this, your code may compile without error, and your program may even appear to run and work correctly. However, you'd be relying on what's called "Undefined Behaviour", and your code could stop running or even crash at any time, due to any number of random, unpredictable factors. Even if it looks like it's okay, it's invalid. Don't do it!

Solution

Fortunately, there is a way to allocate a block of memory with the right size for your elements, when you don't know the elements until your program runs. It's called "dynamic allocation", and it involves a function call:

int *d = malloc(sizeof(int) * (b-a+1));

You can use the same syntax (d[k]) to access "elements" in this "array" or block of memory, but you must later manually free the memory:

free(d);

Possible problem 3

Your assignment says to return an array from the function, but you're not doing this. Instead, you're just creating, filling and printing the array all within the same function (which seems a bit pointless).

You can't actually return an array either, but since you're dynamically allocating the space for it, you have a pointer to work with. It's my opinion that your teacher may have wanted you to return a pointer to this array.

If so, the finished code looks a bit like this:

#include <stdio.h>
#include <stdlib.h>

int *exc(int a, int b)
{

  int i, k = 0;
  int *d = malloc(sizeof(int) * ((b-a)+1));

  for (i=a; i<=b; i++) {
     d[k]=i;
     k++;
  }

  return d;
}

int main(void)
{
 int a,b,i,*ar;

 printf("Give first integer: ");
 scanf("%d",&a);
 printf("Give second integer: ");
 scanf("%d",&b);

 ar = exc(a,b);

 for (i=0; i < (b-a+1); i++) {
    printf("%d ", ar[i]);
 }

 free(ar);

 system("pause");
 return 0;
}

Disclaimer: I'm rusty on C, so the finished code might have a few syntax bugs.

Hope this helps!

like image 74
Lightness Races in Orbit Avatar answered Oct 04 '22 21:10

Lightness Races in Orbit


The size of d is always 0. Since you are initializing it as d[k]. You should instead do something like d[b-a+1].

Update:

Furthermore, the order of your statements are wrong, see pmg's answer.

Update 2:

Your code doesn't actually return the array you are creating and it won't work unless you create the array on the heap (ie. using malloc / free).

like image 39
GWW Avatar answered Oct 04 '22 22:10

GWW