Home > front end >  Print descending array C
Print descending array C

Time:10-27

I have to create functions for print array, fill array witn descending numbers.

I created functions for printing array and creating descending array.But I faced with a problem. If I use my own function printArray() it prints something unclear. Where is the problem, what i do wrong? Please, help.

Here is the code in C. value - is value of array

Function for printing array:

void printArray (int arr[]){
   int i;
   printf("\n");
     for(i = 0; i < value; i   )
       printf("= ", arr[i]);
}

Function for creating descending array:

int createDescendingArray(int a[])
{
    int i;
    printf("\nDescending array is created.\n");
    for (i = value; i > 0; i--) {   
        a[i] = i;
    }
  printArray(a); // print of created array
}

Main function:

int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}

enter image description here

However when I don't use my print function in function createDescendingArray() and print it in Main funktion with standart method like this:

{int i;
 for(i = 0; i < value; i  )
 {
 a[i]=i;
 printf("=", a[i]);
 }
}

It shows descending array as ascending (look at the picture) How it works?

enter image description here

CodePudding user response:

The function

int createDescendingArray(int a[])
{
    int i;
    printf("\nDescending array is created.\n");
    for (i = value; i > 0; i--) {   
        a[i] = i;
    }
    printArray(a); // print of created array
}

is wrong.

According to the output in your question, it seems that you have defined value as 4 (you are not showing us the code with the definition). In that case, your code for the mentioned function is equivalent to the following:

int createDescendingArray(int a[])
{
    printf("\nDescending array is created.\n");

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

    printArray(a); // print of created array
}

I did nothing else to the code than unroll the loop.

Since the array a has a size of 4 elements, valid indices are from 0 to 3. Therefore, by writing to a[4], you are writing to the array out of bounds, causing undefined behavior.

If you had written

for (i = value - 1; i >= 0; i--)

instead of

for (i = value; i > 0; i--)

then the unrolled loop would be:

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

This is better, because now we have fixed the undefined behavior; you are no longer writing to the array out of bounds. However, this is still not what you want. If you want descending output, your unrolled loop must look like this instead:

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

This can be accomplished by changing your function to the following:

int createDescendingArray(int a[])
{
    int i;

    printf( "\nDescending array is created.\n" );

    for ( i = 0; i < value; i   ) {   
        a[i] = value - i - 1;
    }

    printArray(a); // print of created array
}

Here is a small test program:

#include <stdio.h>

//NOTE: It is customary for constants to be written upper-case,
//not lower-case, so the line below should normally not be used.
#define value 4

void printArray (int arr[]) {
   int i;

   printf( "\n" );

   for( i = 0; i < value; i   )
       printf("= ", arr[i]);
}

int createDescendingArray(int a[])
{
    int i;

    printf( "\nDescending array is created.\n" );

    for ( i = 0; i < value; i   ) {   
        a[i] = value - i - 1;
    }

    printArray(a); // print of created array
}

int main( void )
{
    int array[value];
    createDescendingArray( array );
}

The output is:


Descending array is created.

  3   2   1   0 

In this test program, I took over most of your other code, but I did not take over the function main, because it was also causing undefined behavior:

int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}

In the line

arr1[value] = createDescendingArray (arr1);

you are assigning the return value of the function to a variable, although the function did not return a value. This causes undefined behavior. You may want to consider changing the return type to void in the function declaration, if it does not return a value.

Also, even if the function did return a value, arr1[value] would be writing to the array out of bounds, as valid indices are from 0 to value - 1.

CodePudding user response:

You have been using a variable named value in your function which prints array, without initializing it, hence the garbage value.

you should initialize it in the function or pass its start value as an argument to the function.

void printArray (int arr[], int length){
   int i;
   printf("\n");
   for(i = 0; i < length; i  ) {
      printf("= ", arr[i]);
   }
}
int* createDescendingArray(int length)
{

    if (length == 0) return -1;
    int* a = new int[length];
    printf("\nDescending array is created.\n");
    for (int i = length; i >= 0; i--) {   
        a[i] = i;
    }
    printArray(a, length); // print of created array
    return a;
}

these changes should most probably do the trick but again, there is no initialization of value in the function that creates array as well

EDIT: stop creation of array if length is 0

EDIT2: fixed code to consider 0 as an element

  • Related