1

BELOW IS THE ASSIGNMENT:

Write a program to print the last element in an array of doubles. The array should have a dynamic length. The length should be taken from a command line argument. If no command line argument is provided to your program, the array length should be 5. The array should be created dynamically and filled with values starting with 0.0 -> 0.1 -> 0.2 ......

Example:

Call your program: mycalc.exe 42

Output of your program will be in this example: My last array element is: 4.1


However, when I run this code, it always shows 0.0 no matter how I change my command line argument or even I have no command line argument. What is the problem, can anybody correct my code, please. I'm in a hurry, thank you so much.

BELOW IS THE CODE I WROTE:

#include <stdio.h>  // standard input/output functions
#include <string.h>
#include <stdlib.h>

#define last_digit_of_Matrikelnummer 5

int main(int argc, char* argv[])
{
    int i;
    int DynArraylen;
    double* DynArray;

    

    if (argc == 1)
    {
        DynArraylen = last_digit_of_Matrikelnummer;
        DynArray = (double*)malloc(DynArraylen * sizeof(double));
        DynArray[0] = 0.0;

        for (i = 1; i < DynArraylen; i++)
        {
            DynArray[i] = DynArray[i - 1] + 0.1;
        }
        printf("My last array element is: %.1f\n", DynArray[i]);

        //Free memory
        free(DynArray);
    }
    else if (argc == 2)
    {
        //Dynamic memory allocation
        DynArraylen = (int)argv[1];
        DynArray = (double*)malloc(DynArraylen * sizeof(double));
        DynArray[0] = 0.0;

        //check 
        if (DynArray == NULL)
        {
            printf("out of memory\n");
        }
        else
        {
            DynArray[0] = 0.0;
            for (i = 1; i < DynArraylen; i++)
            {
                DynArray[i] = DynArray[i - 1] + 0.1;
            }
            printf("My last array element is: %.1f\n", DynArray[i]);
        }

        //Free memory
        free(DynArray);
    }


    return 0;
}
  • 1
    `printf("My last array element is: %.1f\n", DynArray[i]);` last arg should be `DynArray[i-1])` or `DynArray[DynArraylen-1]` – kaylum May 07 '21 at 01:49
  • 1
    `DynArraylen = (int)argv[1];` That's not correct the way to convert a string to an int. [How to convert a string to integer in C?](https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c) – kaylum May 07 '21 at 01:51
  • 2
    OT: You shouldn't repeat the same code in each conditional block. The only line of code that needs to be conditional is the one that sets `DynArrayLen`. After that all the code is the same. – kaylum May 07 '21 at 01:54

1 Answers1

1

After the loop

for (i = 1; i < DynArraylen; i++)

the value of i will be equal to DynArraylen. Which is one out of bounds.

You need to use i - 1 after the loop (or DynArraylen - 1) to print the last element.


And as mentioned (int)argv[1] is not the correct way to convert a string to a number. For that you need to use the strtol function:

DynArraylen = strtol(argv[1], NULL, 10);

The strtol function includes validation of the string, which you really need to check for to make sure that DynArraylen is valid after the call.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Why is it that case? In loop "for (i = 1; i < DynArraylen; i++)" , i will end up (DynArraylen-1) because i < DynArraylen, then why using i - 1 for final printf is correct? – Chang-Min Yu May 07 '21 at 02:03
  • 1
    @Chang-MinYu The loop continue while `i < DynArraylen` is true. That means it will end when `i == DynArraylen` (as then `i < DynArraylen` will be false). – Some programmer dude May 07 '21 at 02:06