Will these print the same thing?

Slides:



Advertisements
Similar presentations
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.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
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;
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
CS 160 Final Review. Name 4 primitive type variables?
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Computer Organization and Design Pointers, Arrays and Strings in C
CS 160 Final Review.
Computer Science 210 Computer Organization
Functions and Pointers
Formatted Input/Output
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CS1010 Programming Methodology
Chapter 2 Overview of C.
Review of C… The basics of C scanf/printf if/elseif statements
Quiz 11/15/16 – C functions, arrays and strings
Lecture-5 Arrays.
Functions Dr. Sajib Datta
Pointers.
FUNCTIONS.
Module 2 Arrays and strings – example programs.
Formatted Input/Output
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CS1100 Computational Engineering
Lesson 2: Building Blocks of Programming
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
In Class Program: Today in History
CSCE 206 Lab Structured Programming in C
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Parameter Passing in Java
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers.
Your questions from last session
Function In this lesson, you will learn about Introduction to Function
Arrays.
Functions.
Suggested self-checks: Section 7.11 #1-11
EECE.2160 ECE Application Programming
Let’s start from the beginning
Strings #include <stdio.h>
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CSCE 206 Lab Structured Programming in C
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Functions I Creating a programming with small logical units of code.
CSCE 206 Lab Structured Programming in C
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Structures, Unions, and Enumerations
Introduction to Problem Solving and Programming
Presentation transcript:

Will these print the same thing? #include <stdio.h> void printout(int num[]); int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<9;i++) printf(“num=%d”, num[i]); #include <stdio.h> int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; int i; for (i=0;i<9;i++) printf(“num=%d”, num[i]); } YES!

What will this print out? #include <stdio.h> void change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; change(array, val); printf(“array=%d, val=%d”,array[0],val); } void change(int array[], int val) int i; array[0] = array[0]*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

In the previous example, array was passed by ____1______, while val was are passed by ___2_____? 1. reference, value 2. value, reference 4. Oh shoot, don’t know, I’d better ask Garvin’s other half as she is the one who knows everything! 3. Yo mamma, yo daddy

What will this print out? #include <stdio.h> void change(int array, int val); int main(void) { int val = 3; int array[1] = {3}; change(array[0], val); printf(“array=%d, val=%d”,array[0],val); } void change(int not_an_array, int val) not_an_array = not_an_array*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6 Notice that the function no longer takes in an array, but an array element – which is passed by value

What will this print out? #include <stdio.h> int change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; val = change(array, val); printf("array=%d, val=%d",array[0],val); } int change(int array2[], int val) array2[0] = array2[0]*2; val = val * 2; return val; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

The function printout does not know anything about variable size What is missing here? #include <stdio.h> void printout(int num[]); int main(void) { int size = 9; int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]); The function printout does not know anything about variable size

So the previous examples needs to look like one of these two: #include <stdio.h> void printout(int num[], int size); int main(void) { int size = 9; int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num, size); } void printout(int num[], int size) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]); #include <stdio.h> #define size 9 /* include a define */ void printout(int num[]); int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]);

Do you need to know the array size of a character array before hand? 1. No because a character array is a string with a terminating character, ‘\0’, and thus you can just use strlen 2. Yes because it is an array and every array has a length that needs to be known by the programmer beforehand 3. Yes because it is I think I read somewhere that says I must know the array size of a character array beforehand – some blog I think. 4. What is Garvin’s wife’s number, I’ll just ask her.

Skeleton for your inclass homework: /* include all include files that you need */ void changearray( you must figure out what goes here); int main() { /* Declare variables */     /* Read in array elements from user of a specified size (arrayLength) */     for (i = 0; i < arrayLength; i++)     {         printf("Please enter an integer to be put in the array: ");         scanf("%d", &array[i]);     } /* print out array */ /* call the changearray function */ /* print out new array */ } void changearray(you must figure out this function)