Week 3 Lecture Code

// Pantea Aria


// Enum

// Imagine you are working in a coffee shop ordering system.  
// We can make a type for coffee sizes with limited values, size_type  
// (instead of using characters like 'S', 'M', 'L').  
//
// enum size_type {SMALL, MEDIUM, LARGE};  
// 0 for SMALL  
// 1 for MEDIUM  
// 2 for LARGE  

// Write a program that declares a variable of type enum coffee
// and assigns it the value LARGE. Then, print the value.



#include <stdio.h>

enum coffee {SMALL, MEDIUM, LARGE}; 

int main(void) {

    // get memory for your coffee
    // declare a var of type enum coffee
    enum coffee my_coffee;
    my_coffee = LARGE; 
    // my_coffee = 2;
    if (my_coffee == LARGE) {
        printf ("the size of my coffe is Large\n");
    } else if (my_coffee == SMALL) {
        printf ("the size of my coffe is Small\n");
    } else {
         printf ("the size of my coffe is Medium\n");   
    }    
    
    return 0;

}    
// Pantea Aria

// Nested Loops  recap

// write a program that prints a 6 x 5 grid of characters

// A B C D E
// A B C D E
// A B C D E
// A B C D E
// A B C D E
// A B C D E

#define COLS 5
#define ROWS 6
#include <stdio.h>

int main(void) {

	// just print out one line of A B C D E
	
	// make a loop to print out letters right after each other
	
	int rows = 0;
	while (rows < ROWS){

		char letter = 'A';
		// know how many times you want to repeat
		// loop with counter
		int i = 0;
		while (i < COLS) {
			printf ("%c ", letter);
			letter = letter + 1;
			i = i + 1;

		}
		printf ("\n");
		rows = rows +1;

	}

	





	return 0;
}
// Pantea Aria


// Struct and Enum


// Imagine you are designing a program for a small library system.  
// Each book has:  
//   - an ID number (int)  
//   - a price (double)  
//   - a category (enum book_type) that can be 
//           FICTION, NONFICTION, or TEXTBOOK  
//
// 0 for FICTION  
// 1 for NONFICTION  
// 2 for TEXTBOOK  
//
// Task:  
// Define an enum and a struct to represent a book.  
// Then, create a book with ID 101, price 59.99, and category TEXTBOOK.  
// Finally, print the book’s details.  

enum book_type {FICTION, NONFICTION,TEXTBOOK };

struct book {
    int id;
    double price;
    enum book_type category;
};

int main(void) {
    struct book my_book;

    //  ID 101, price 59.99, and category TEXTBOOK.  
    
    my_book.id = 101;
    my_book.price = 59.99;
    my_book.category = TEXTBOOK;
    // Finally, print the book’s details. 
    printf ("the id of my book is %d\n", my_book.id);
    // your turn to continue

}
// Pantea Aria 

// functions
// write a function that prints "Hello World!"

#include <stdio.h>


// function prototype
void hello(void);
int main(void) {

    printf ("Hello world from the main function!\n");
    // call function hello
    hello();
    // call again
    hello();

    return 0;

}
// function definition/body
void hello(void) {
    printf ("Hello World from function hello\n");
    return;
}
    
 
// Pantea Aria 

// void functions
// a function to get two integers and print out their average 

#include <stdio.h>

// function prototype
double average (int number1, int number2);
int main(void) {

    int num1 = 10;
    int num2 = 20;

    // call function average and pass num1 and num2 to it
    // also the function returns a double value, so you must have a place for it too
    double avg = average(num1, num2);
    printf ("The average of num1 and num2 is %lf\n", avg);

    // you can also call a non void function in another function like printf
    printf ("The average of 11 and 12 is %lf\n", average (11, 12));

    return 0;
}
// function definition/body
double average (int number1, int number2) {
    double average_of_numbers = (number1 + number2) / 2.0;

    return average_of_numbers;
    // you can also return it directlyand don't use an extra variable
    // return (number1 + number2) / 2.0;
}    
// Pantea Aria 

// functions
// a function to add two integers and return the results to the main

#include <stdio.h>

int main(void) {
    

    
// Pantea Aria 


// functions 

// Define a struct called product that contains:

// am int id 
// a double price

// Write a program that: Creates a product variable in main with some values

//Passes the product to a function

//The function prints the product id and price

#include <stdio.h>

int main(void) {
// Pantea Aria

// function recap - void function/procedure


// Write a function called print_even_or_odd that:
// Takes an integer as input, you pass an integer to the function
// Prints whether the number is even or odd
// Does NOT return a value
// What is the type of the function? void

#include <stdio.h>

// function prototype

void print_even_or_odd(int number);

int main(void) {
    int number = 10;

    // function call
    print_even_or_odd(number);   // prints: even
    print_even_or_odd(5);        // prints: odd
    print_even_or_odd(number + 5);
    return 0;
}
// function definition
void print_even_or_odd(int pantea) {
    if (pantea % 2 == 0) {
        printf ("Even\n");
    } else {
        printf("Odd\n");
    }       
    return;

}
// Pantea Aria 

// functions recap

// Write a function called is_neg_or_pos that:
// Takes an integer as input
// Returns 1 if the number is positive
// Returns 0 if the number is negative or zero
// what is the type of the function? int 

#include <stdio.h>
// function prototype
int is_neg_or_pos(int number);

int main(void) {
    int number = 10;
    
    int result = is_neg_or_pos(number);    //returns 1
    
    printf ("function returns %d\n", result);  // output 1
    printf ("Result of is_neg_or_pos(-5) is %d\n", is_neg_or_pos(-5));   // output 0
    
    result = is_neg_or_pos (0);  // returns 0
    printf ("function returns %d\n", result);   // output 0

    return 0;
}
// function definition
int is_neg_or_pos(int number) {

    if (number > 0) {
        return 1;
    } else {
        return 0;
    }
}

    
    
    
    
    
    
    
// Pantea Aria


// static arrays, declare, initialise, read, print

// when you need to store multiple values of the same type

#include <stdio.h>

int main(void) {

    //declare an int array of size 5
    int numbers[5];

    // assign values to all elements
    numbers[0] = 10;
    numbers[1] = 20;
    numbers[2] = 30;
    numbers[3] = -90;
    numbers[4] = 0;    
    // print out the first and third values from your array
    printf("%d %d", numbers[0], numbers[2]);     
    
    // input integers into the first two elements of array
    printf ("Enter the first and second numbers:");
    scanf("%d %d", &numbers[0], &numbers[1]);
    // read 5 new integers into this array
    int i = 0;
    printf ("Enter 5 ints:");
    while (i < 5) {
        scanf("%d", &numbers[i]);
        i++;
    }
    // print out all elements of the array
    // don't forget to return to the first index
    // or use another variable int j = 0;
    i = 0 ;
    while (i < 5) {
        printf ("%d ", numbers[i]);
        i++;
    }
    printf ("\n");
    

    return 0;  
}   
// Pantea Aria 

// functions, arrays

// Write a function called has_zero that:
// Takes an array of integers and its size as input (what is the prototype of this function?)
// Returns the number of 0 in the array

// function prototype
int has_zero(int array[], int length);

#include <stdio.h>

int main(void) {

    int array[] = {2, 0, 5, -3, 0 , 0, 4};
    // int result = has_zero(array, 7);
    printf ("the number of zeros is %d\n", has_zero(array, 7));

    return 0;
}     
// function body/definition
int has_zero(int array[], int length) {
    int i = 0;
    int number_of_zeros = 0;
    while (i < length) {
        if (array[i] == 0) {
            number_of_zeros = number_of_zeros + 1;
        }
        i++;
    }
    return number_of_zeros;
}    
    
    
    
// Pantea Aria 

// functions
// write a function that prints "Hello Worl!"

#include <stdio.h>



int main(void) {

    }
    
  
    
 
// Pantea Aria 

// void functions
// a function to get two integers and print out their average 

#include <stdio.h>

int main(void) {
// Pantea Aria 

// functions
// a function to add two integers and return the results to the main

#include <stdio.h>

int main(void) {
    

    
// Pantea Aria 


// functions 

// Define a struct called product that contains:

// am int id 
// a double price

// Write a program that: Creates a product variable in main with some values

//Passes the product to a function

//The function prints the product id and price

#include <stdio.h>

int main(void) {
// Pantea Aria

// function recap - void function/procedure


// Write a function called print_even_or_odd that:
// Takes an integer as input
// Prints whether the number is even or odd
// Does NOT return a value
// What is the type of the function? void

#include <stdio.h>

// function prototype


int main(void) {
    int number = 10;

    // function call
    print_even_or_odd(number);   // prints: 10 is even
    print_even_or_odd(5);        // prints: 5 is odd

    return 0;
}
// function definition
// Pantea Aria 

// functions recap

// Write a function called is_even_or_odd that:
// Takes an integer as input
// Returns 1 if the number is even
// Returns 0 if the number is odd
// what is the type of the function? int 

#include <stdio.h>
// function prototype


int main(void) {
    int number = 10;
    
    int result = is_even_or_odd(number);    //returns 1
    
    printf ("function returns %d\n", is_even_or_odd(5));  // output 0
    printf ("Result of is_even_or_odd(10) is %d\n", result);   // output 1
    
    return 0;
}
// function definition

    
    
    
    
    
    
    
// Pantea Aria


// static arrays, declare, initialise, read, print

// when you need to store multiple values of the same type

#include <stdio.h>

int main(void) {

    //declare an int array of size 5

    // assign values to all elements
    
    // print out the first and third values from your array     
    
    // input integers into the first two elements of array
    
    // read 5 new integers into this array

    // print out all elements of the array

    return 0;  
}   
// Pantea Aria 

// functions, arrays

// Write a function called has_zero that:
// Takes an array of integers and its size as input (what is the prototype of this function?)
// Returns the number of 0 in the array

#include <stdio.h>

int main(void) {

    int array[] = {2, 0, 5, -3, 0 , 0, 4};
    

    return 0;
}