Lab04 - Arrays

Task 1

  • A command-line argument specifies the number of integers to read (N)
  • Read N integers from standard input using scanf() to a variable-length array of size N
  • Print the array in the order entered
  • Sort the array in descending order using qsort()
  • Print the sorted array

Task 2

  • Write a program which allows the user to enter an “infinite” number of characters and will keep appending them to an array.
  • When they press P, it will output the current array.
  • If they enter a character X, it will convert the chars to upper case with toupper(), print them and exit.
  • Use the getchar() function to get letters from the command line.
  • You will need to use malloc() and realloc() to control the memory.
  • Don’t forget to use free() at the end to free the memory.
  • If you have time, add also sorting the chars alphabetically and lower case before upper case.

Valgrind

  • use valgrind for memory leak detection
  • can detect forgotten allocated memory and the exact location of out-of-bound access
  • can report using uninitialized variables/memory
  • for better output, compile your program with -g flag
  • valgrind ./your-program

Documentation

  • Learn to understand the function declaration (the name, return type, and parameters)
  • The definition (code to be executed) might not always be available
courses/be5b99cpl/labs/lab04.txt · Last modified: 2025/10/15 10:34 by ulricji1