Week 4 Lecture Code

// Pantea Aria

// 1D array recap

// read the score of 5 quizess
// print out all integers first, then the average
// Write code to calculate and print the average quiz score.
// 10 20.5 30 40.9 50
// avg = (10 + 20.5 + 30 + 40.9 + 50) / 5


#include <stdio.h>
#define MAX 5
int main(void) {

    // declare an array of size 5 of type double
    double quiz[MAX];

    // to repeat reading integers into all elements of my array
    // I need a loop
    int i = 0;
    double sum = 0.0;
    while (i < MAX) {
        scanf ("%lf", &quiz[i]);
        sum = sum + quiz[i];
        i = i + 1;
    }
    // i is now 5
    double avg = sum / 5;
    // repeat printing for 5 times
    // I need a loop
    i = 0;
    // int j = 0;
    printf ("The average of ");
    while(i < MAX) {
        printf("%.2lf ", quiz[i]);
        i = i + 1;
    }
    printf ("is %lf\n", avg);
    return 0;

}    

   
// Pantea Aria

// 1D array and functions recap
// read size of the array from user

// write functions to 
    // read size integer to an array of int
    // print out all data of the arrays
    // find the maximum and return to main


#include <stdio.h>
void input(int size, int numbers[]);
int maximum(int size, int numbers[]);
void output(int size, int numbers[]);
int main(void) {

    // declare the array of size
    int size;
    printf ("Enter size of the array:");
    scanf ("%d", &size);
    printf ("The value of size is: %d", size);
    
    int numbers[size];
    // call function input to read size integer to an array of int
    input(size, numbers);

    // call output to print out all data of the arrays
    output(size, numbers);
    // find the maximum and return to main
    // call a non void function inside printf
    printf("The maximum of all is %d\n", maximum(size, numbers));

    return 0;
}    
void input(int size, int numbers[]){
    int i = 0;
    printf ("Enter %d integers:", size);
    while(i < size) {
        scanf("%d", &numbers[i]);
        i++;
    }
    return;
}
void output(int size, int numbers[]){
    int i = 0;
    printf ("All elements are:");
    while(i < size) {
        printf("%d ", numbers[i]);
        i++;
    }
    printf ("\n");
    return;
}
int maximum(int size, int numbers[]) {

    int max = numbers[0];
    // then go the whole array
    // and compare all other elements with max
    int i = 0;
    while (i < size) {
        if (max < numbers[i]) {
            max = numbers[i];
        }
        i++;
    }
    return max;
}  
// Pantea Aria

// array of struct - collection of products

// write a program that stores information for 5 products using a struct with two fields:
// id (integer) 
// price (double)
  
// After reading the data, your program should:
// pass the array to a function to calculate the total price of the 5 products.
// display the total

#include <stdio.h>
#define MAX 5

struct product {
    int id;     // id (integer) 
    double price;   // price (double)  
};  
void input(int size, struct product numbers[]);
double calculate(int size, struct product numbers[]);
int main(void) {
    
    // declare an array of size MAX of type product
    struct product products[MAX];
    // read information of 5 products
    input(MAX, products);
    // call calculate, and return the total price
    double total_price = calculate(MAX, products);

    printf ("Total price is %lf\n", total_price);
    return 0;
}       
void input(int size, struct product numbers[]){
    int i = 0;
    printf ("Enter %d product id and price:", size);
    while(i < size) {
        scanf("%d %lf", &numbers[i].id, &numbers[i].price);
        i++;
    }
    return;
}
double calculate(int size, struct product numbers[]) {
    double total = 0;
    int i = 0;
    while(i < size) {
            total = total + numbers[i].price;
            i++;
    }
    return total;
}
// Pantea Aria


// static 2D arrays: similar to a Matrix/array

// Write a program that reads a 2x3 integer matrix from the user,
// then prints it in matrix form (2 rows, 3 columns).


#include <stdio.h>
#define ROWS 2
#define COLS 3
int main(void) {
    int matrix[ROWS][COLS];
    // repeat reading for 6 elements (2 x 3)
    // I need one loop for the number of rows
    // loop for the number of cols
    int i = 0;
    while (i < ROWS) {
        int j = 0;
        while(j < COLS) {
            printf("Enter an integer:");
            // reading for row i, col j
            scanf("%d", &matrix[i][j]);
            j++;
        }
          i++;  
    }
    // then prints it in matrix form (2 rows, 3 columns).
    i = 0;
    while (i < ROWS) {
        int j = 0;
        while(j < COLS) {
            
            printf ("%5d ", matrix[i][j]);
            j++;
        }
        printf("\n");
        i++;  
    }


   return 0;

}    
    
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints all negative elements, one row per line, space-separated.
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
#include <stdio.h>
int main(void) {
    int array[3][3] =  {{1, 0, -1}, {2, -2, 4}, {0, 5, 3}};
    int i = 0;
    while(i < 3) {
        int j = 0;
        while(j < 3) {
            if (array[i][j] < 0) {
                printf("%3d\n", array[i][j]);
            }
            j++;
        }
        i++;
    }


}
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints the sum of all elements with a function.

#include <stdio.h>
int main(void) {
    int array[3][3] =  {{1, 0, -1}, {2, -2, 4}, {0, 5, 3}};
    int i = 0;
    int sum = 0;
    while(i < 3) {
        int j = 0;
        while(j < 3) {

            sum = sum + array[i][j];
            j++;
        }
        i++;
    }
    printf ("Sum is: %d\n", sum);
    return 0;
}       
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following square 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints the sum of all main diagonal elements with a function.
// The main diagonal runs from the top-left to the bottom-right.

// your turn: sum of all rows
// your turn: sum of all columns
// your turn: sum of all secondary diagonal elements (top-right to the bottom left)

#include <stdio.h>
int main(void) {
    int array[3][3] =  {{1, 0, -1}, {2, -2, 4}, {0, 5, 3}};
    int i = 0;
    
    while(i < 3) {
        int j = 0;
        int row_sum = 0;
        while(j < 3) {
            row_sum = row_sum + array[i][j];      
            j++;   
        }
        // what is row_sum?
        // it is the sum of all integers in row i
        printf("Sum of all int in row %d is %d\n", i, row_sum);
        i++;
    }
    
    return 0;
}    
// Pantea Aria


// static 2D arrays recap

// You are given two 2D arrays of integers (table1 and table2)
// with the same number of rows and columns.
// Write a function called add_tables that:
// Takes table1, table2, and sum as parameters
// Adds the corresponding elements of table1 and table2
// Stores the sum in result
// Write a function called print_table that prints a 2D array in grid form.

#include <stdio.h>

#define ROWS 2
#define COLS 3

void print_array(int array[ROWS][COLS]);
void add_tables(int table1[ROWS][COLS], 
                int table2[ROWS][COLS], 
                int sum[ROWS][COLS]);
int main(void) {
    int table1[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int table2[ROWS][COLS] = {
        {7, 8, 9},
        {1, 2, 3}
    };

    int sum[ROWS][COLS];

    // call function add_tables
    add_tables(table1, table2, sum);

    

    printf("Table 1\n");
    // call function print_table
    print_array(table1);


    printf("\nTable 2\n");
    // call function print_table
    print_array(table2);

    printf("\nSum Table\n");
    // call function print_table
    print_array(sum);
}
void add_tables(int table1[ROWS][COLS], 
                int table2[ROWS][COLS], 
                int sum[ROWS][COLS]) {

    int i = 0;
    while(i < ROWS) {
        int j = 0;
        while(j < COLS) {
            sum[i][j] = table1[i][j] + table2[i][j];
            j++; 
        }
        i++;
    }
    return;
}
void print_array(int array[ROWS][COLS]) {
    int i = 0;
    while(i < ROWS) {
        int j = 0;
        while(j < COLS) {
           
            printf("%3d", array[i][j]);
            
            j++;
        }
        printf("\n");
        i++;
    }
}
// Pantea Aria

// characters and strings
// declare, initialise, print

#include <stdio.h>

int main(void) {
  
    // Which of these is a valid string in C? Why?
    char name[] = {'J', 'o', 'e', '\0'}; 
    // char name[] = "Joe";
    // char name[] = {'J', 'o', 'e'};   //not correct
    
    // What will this output?
   // error name = "Anna";
    printf("%s\n", name);   //Anna
    
    // How many elements are in the array below?  4
    // char name[] = "Tom";

    // print out name
    printf("%s\n", name);

    // or 

    ///int i = 0;
    //while(name[i] != '\0') {
        // what is name[i] at the beginning ? 'T'
      //  printf("%c", name[i]);
        //i++;
    //}




    return 0;
    
}    
// Pantea Aria

// strings - reading - working on chars one by one
// ctype.h library functions

// Write a C program that reads a line of characters from the user and 
// send it to a function, converts all uppercase letters to lowercase.

// can you write it for multiple books? until user enters Ctrl-D


#include <stdio.h>
#include <ctype.h>

#define MAX 30

void my_tolower(char str[]);
int main(void) {
    char book[MAX];  // means book is an empty string now

    // how can I repeat this?
    printf("Enter your favourite book: ");
    while (fgets(book, MAX, stdin) != NULL) {  
        
        printf("Original: %s", book);

        // call the function
        my_tolower(book);
        printf("Lowercase: %s", book);

        printf("Enter your favourite book: ");

    }
    return 0;
}
void my_tolower(char str[]) {
    int i = 0;
    while(str[i] != '\0' && str[i] != '\n'){
        str[i] = tolower(str[i]);
        i++;
    }
}
// Pantea Aria

// strings - strlen - strcpy - strcmp
// compare two strings

#include <stdio.h>
#include <string.h>
#define MAX 10
int main(void) {

    char input1[] = "Hello";
    char input2[MAX];
    
    // strcpy input1 to input2
    strcpy(input2, input1);
    
    
    // what is the length of input1?
    printf ("The length of input1 is %lu\n", strlen(input1));

    // compare the strings above with strcmp
    // show the output
    printf ("strcmp(input1, input2) returns %d\n",strcmp(input1, input2));
    
    
    // fgets a new input, enter "Pantea"
    // then strcmp with "Pantea"
    // is there any issue? Next Week
    
    
    
    
    return 0;
}    
    
// Pantea Aria

// 1D array recap

// read the score of 5 quizess

// Write code to calculate and print the average quiz score.
// Which index stores the highest quiz score?


#include <stdio.h>
#define MAX 5
int main(void) {
    
          
    return 0;
}    

   
// Pantea Aria

// 1D array and functions recap

// write functions to 
// read SIZE integer to an array of int
// print out all data of the arrays
// find the maximum and return to main


#include <stdio.h>
#define SIZE 5

int main(void) {
    
          
    return 0;
}    

   
// Pantea Aria

// array of struct - collection of products

// write a program that stores information for 5 products using a struct with two fields:
// id (integer) 
// price (double)
  
// After reading the data, your program should:
// pass the array to a function to calculate the total price of the 5 products.
// display the total

#include <stdio.h>
#define MAX 5

struct product {
    int id;     // id (integer) 
    double price;   // price (double)  
};  

int main(void) {
    
    
    printf ("Total price is %lf\n", total);
    return 0;
}       
// Pantea Aria


// static 2D arrays: similar to a Matrix/array

// Write a program that reads a 2x3 integer matrix from the user,
// then prints it in matrix form (2 rows, 3 columns).


#include <stdio.h>
#define ROWS 2
#define COLS 3
int main(void) {
   
    

         
    return 0;
}    
    
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints all negative elements, one row per line, space-separated.
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints the sum of all elements with a function.

#include <stdio.h>
#define ROWS 3
#define COLS 3

int main(void) {
    
          
    return 0;
} 
// Pantea Aria


// static 2D arrays: similar to a Matrix

// Write a program that declares the following 2D array of integers
//     {
//       {1, 0, -1},
//       {2, -2, 4},
//       {0, 5, 3}
//     }
// and prints the sum of all elements with a function.

// your turn: sum of all rows
// your turn: sum of all columns

#include <stdio.h>
#define ROWS 3
#define COLS 3

int main(void) {
    
          
    return 0;
} 
// Pantea Aria


// static 2D arrays recap

// You are given two 2D arrays of integers (table1 and table2)
// with the same number of rows and columns.
// Write a function called add_tables that:
// Takes table1, table2, and result as parameters
// Adds the corresponding elements of table1 and table2
// Stores the sum in result
// Write a function called print_table that prints a 2D array in grid form.

#include <stdio.h>

#define ROWS 2
#define COLS 3


int main(void) {
    int table1[ROWS][COLS] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int table2[ROWS][COLS] = {
        {7, 8, 9},
        {1, 2, 3}
    };

    int sum[ROWS][COLS];

    // call function add_tables
    

    printf("Table 1\n");
    // call function print_table

    printf("\nTable 2\n");
    // call function print_table

    printf("\nSum Table\n");
    // call function print_table
// Pantea Aria

// characters and strings
// declare, initialise, print

#include <stdio.h>

int main(void) {
  
    // Which of these is a valid string in C? Why?
    char name[] = {'J', 'o', 'e', '\0'}; null character
    char name[] = "Joe";
    char name[] = {'J', 'o', 'e'};
    
    // What will this output?
    char name[] = "Anna";
    printf("%s\n", name);
    
    // How many elements are in the array below?
    char name[] = "Tom";

    // print out name




    return 0;
    
}    
// Pantea Aria

// strings - reading - working on chars one by one
// ctype.h library functions

// Write a C program that reads a line of text from the user and 
// send it to a function, converts all uppercase letters to lowercase.

// can you write it for multiple books?


#include <stdio.h>
#include <ctype.h>

#define MAX 30


int main(void) {
    char book[MAX];

    printf("Enter your favourite book: ");
    // read a book

    printf("Original: %s", book);

    // call the function
    
    

    printf("Lowercase: %s", book);


    return 0;
}
// Pantea Aria

// strings - strlen - strcpy - strcmp
// compare two strings

#include <stdio.h>
#include <string.h>

int main(void) {

    char input1[] = "Hello";
    char input2[];
    
    // strcpy input1 to input2
    
    
    // what is the length of input1?
    
    
    
    // compare the strings above with strcmp
    // show the output
    
    
    // fgets a new input, enter "Pantea"
    // then strcmp with "Pantea"
    // is there any issue?
    
    
    
    
    return 0;
}