CSC215 Homework Homework 04 Due date: Oct 14, 2016.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Computer Science 210 Computer Organization Introduction to C.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Tutorial ICS431 – Introduction to C.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
WEEK 4 Class Activities Lecturer’s slides.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Introduction to Programming Lecture 5: Interaction.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
1 Advanced Programming Examples. using System; class Test { static void Main( string[] args ) { int a = 7; int b = 3; int c = 5; int d = 9; Console.WriteLine(!(d.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Pseudocode FORTRAN (original) INPUT number
Tutorial #4 Summer 2005.
Pointers verses Variables
EKT120: Computer Programming
Formatted Input and Output
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
IS12 - Introduction to Programming Lecture 19: Functions and Arrays
Functions, Part 2 of 2 Topics Functions That Return a Value
CSE1320 Loop Dr. Sajib Datta
CS1010 Programming Methodology
Pointers.
Module 2 Arrays and strings – example programs.
Introduction to C Programming
Dynamic memory allocation and Intraprogram Communication
מבוא כללי למדעי המחשב תרגול 2
Input and Output Lecture 4.
Formatted and Unformatted Input/Output Functions
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
More Examples of argc and argv
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Looping.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Pointers.
Nhập và Xuất trong C Chương 4 Input and Output in C.
Константе оператори Задаци....
CC213 Programming Applications
Chapter 8 The Loops By: Mr. Baha Hanene.
Introduction to C Programming
A function with one argument
UMBC CMSC 104 – Section 01, Fall 2016
CSC215 Homework Homework 05 Due date: Oct 21, 2016.
CSC215 Homework Homework 05 Due date: Oct 21, 2016.
CSI 121 Structure Programming Language Lecture 9 Selection
Revision.
CSC215 Homework Homework 02 Due date: Sep 30, 2016.
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Arrays.
Recursion.
Strings #include <stdio.h>
Programming Arrays.
CSCE 206 Lab Structured Programming in C
Iteration Statement for
More on iterations using
Presentation transcript:

CSC215 Homework Homework 04 Due date: Oct 14, 2016

Question 1 : Write C functions that perform the given tasks A function isprime that takes a positive integer as an input and produces 1 if it is a prime number and 0 otherwise. Example: printf("%d\n", isprime(17)); /* output is: 1 */ A function slen that takes a string as an input and return the length of the string (i.e. its letters count). Example: printf("%d\n", slen("Whatever")); /* output is: 8 */ A recursive function reverse that reads a sequence of positive integer numbers and when reads a non-postive value prints the sequence in a reversed order. Example: reverse(); /*input: 12 3 27 55 9 -1 output: 9 55 27 3 12 */ A function sumdig that reads a positive integer and returns the sum of its digits. Example: printf("%d\n", sumdig(628105)); /* output is: 22 */

Question 1 : Answers int isPrime(int x){ 3 int i; for (i =2; i <= x/2; i++) if (x % i == 0) return 0; return 1; } int slen(char* str){ 3 int i=0; while(str[i++] != '\0'); return --i; } void reverse(){ 3 int x; scanf("%d", &x); if (x > 0) reverse(); printf("%d ", x); } int sumdig(int x){ 3 int result = 0; while(x > 0){ result += x%10; x /= 10; } return result; }

Question 2 : What is the output of each of the following programs? #include <stdio.h> void display(); int main(){ display(); display(); } void display(){ static int c = 0; printf("%d ",c); c += 5; } #include <stdio.h> void funct1(void); void funct2(void); int globvar = 10; int main(){ globvar = 20; printf("%d\n", globvar); funct1(); printf("%d\n", globvar); funct2(); printf("%d\n", globvar); return 0; } int globvar2 = 30; void funct1(void){ char globvar; globvar = 'A'; globvar2 = 40; printf("%c %d\n", globvar, globvar2); void funct2(void){ double globvar2; globvar = 50; globvar2 = 1.234; printf("%d %.4f\n", globvar, globvar2); 20 1 A 40 1 20 1 50 1.23402 50 1 0 5 2