Download presentation
Presentation is loading. Please wait.
1
Functions continued
2
Function comments /** * Function: GetIntInRange * * Description: Asks the user for an integer in between the selected limits. * Repeats until requirements are met and returns the number. * Outputs a warning when input is out of range. * Parameters: min - integer, lower limit for the user input (inclusive) * max - integer, upper limit for the user input (inclusive) * Return: Integer number within the specified limits */ 2018 Risto Heinsar
3
More about functions Using global variables is often not recommended, also forbidden during this subject When calling a function, only the variables, which has an important value at that time, should be passed Arrays cannot be returned. However you can pass them. Temporary variables, loop counters etc. should always be local Only one value can be returned. To modify multiple single values, pointers are needed (by reference) – future topic Similar blocks of code (copy-paste with minor differences) should be made into functions. Use variables to handle those differences. 2018 Risto Heinsar
4
Where to declare?* int SumArrayMembers(int numbers[], int arrLen) { int sum, i; sum = 0; for (i = 0; i < arrLen; i++) sum += numbers[i]; } return sum; 2018 Risto Heinsar
5
Where to declare?* int main(void) { double avgBaddiesDisposed; char avengers[AVENGER_LIM][NAME_LIM]; int disposeCount[AVENGER_LIM]; int avengerCount = GetAvengerCount(LOWER_LIM, AVENGER_LIM); FillMissionStats(avengers, disposeCount, avengerCount); avgBaddiesDisposed = FindAvgBaddieDisposedOf(disposeCount, avengerCount); DisplaySlackers(avengers, disposeCount, avengerCount, avgBaddiesDisposed); return 0; } 2018 Risto Heinsar
6
A few reminders and hints
Array length must always be passed! At least (n – 1) array dimensions must be defined, however you can always fix all of them To change the placement of two numbers, you need to swap them using 3 assignment statements Initializing to zero: int vector[LEN] = {0}; int matrix[ROWS][COLS] = {{0}}; 2018 Risto Heinsar
7
A few reminders and hints
printf() vs putchar() vs puts() printf() – print formatted – print formatted text, manual line changes puts() – put string – print plain text, no format, automatic line change putchar() – put character – prints one character Character – between apostrophes e.g. ‘a’, ‘c’, ‘\n’, ‘\b’, ‘\0’ Character array (string) – between quotation marks e.g. "a", „hello" 2018 Risto Heinsar
8
The result of the lab task
Familiarize yourself with the finished program (given) 10_lab_switch_linux Store it on the P drive To run it, go to the location where You stored it using the terminal if „permission denied“ appears, chmod _lab_switch_linux Run using it from the terminal NB! The binary was compiled for the lab computers, might not work everywhere 10_lab_switch_windows.exe Under Windows, You should be able to run this either from the command line or just by opening it up normally Normally you should never run a binary file from an untrusted source 2018 Risto Heinsar
9
Lab task (use basecode, mimic our program)
Generate a matrix (n*m) with random elements. Create limits if necessary. n and m are entered by the user Allow user to display the matrix Only use while() loops Allow user to generate a new matrix with new dimensions Only use do while() loops Task 2: Allow user to switch 2 rows Only use for() loop Allow user to switch 2 columns All operations must be repeatable without exiting the program We’ve created a menu structure for you. You must create the code for each of the menu options Actions must be performed inside of functions (call the from the menu) 2018 Risto Heinsar
10
Advanced Allow the user to remove rows and/or columns
Allow the user to add rows and/or columns at the desired location Allow the user to replace row, column or an element Allow the user to mirror the matrix horizontally and vertically Allow the user to transpose the matrix Create the necessary menu structure and functions for the added functionality 2018 Risto Heinsar
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.