Functions continued.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Introduction to C Programming
Chapter 10.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
ECE Application Programming
User-Written Functions
Topic Pre-processor cout To output a message.
CSE 220 – C Programming C Fundamentals.
Repetition Structures Chapter 9
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
A First Book of ANSI C Fourth Edition
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
C-Programming, continued
Variables and Arithmetic Operations
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Arrays, For loop While loop Do while loop
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
Chapter 14 - Advanced C Topics
Arrays Kingdom of Saudi Arabia
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
File Input and Output.
MSIS 655 Advanced Business Applications Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
7 Arrays.
Functions.
String manipulation string.h library
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Loops.
Fundamental Programming
Bubble sort.
Let’s start from the beginning
Arrays.
Arrays.
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
More Loops Topics Relational Operators Logical Operators for Loops.
Topics Introduction to File Input and Output
Introduction to C Programming
Revision.
EECE.2160 ECE Application Programming
Introduction to Computer Programming IT-104
Presentation transcript:

Functions continued

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

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

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

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

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

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

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 775 10_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

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

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