Notes, summaries, assignments, exams, and exercises for Computer science for Baccalaureate

Essential C Programming Syntax and Memory Management

Computer science62.93 KB

Operators

  • Arithmetic: +, -, *, /, %
  • Relation: ==, !=, >, <, >=, <=
  • Logic: &&, ||, !
  • Assignment: =, +=, -=, *=, /=, ?=
  • Increment: ++, --
  • Order of operations: (), *, /, %, +, -, <>, ==, !=

ASCII Conversions

  • Lowercase to uppercase: 'x' - 32
  • Uppercase to lowercase: 'x' + 32

Data Types

Data TypesExampleSizeFormat
intint age = 19;4 bytes%d
floatfloat a = 3.7;4 bytes%f
doubledouble pi = 3.1415;8 bytes%lf
charchar grader = 'a';1 byte%c
stringchar name[20] = "pierre";array%s

Constants

#define TAX 0.13
const int Max = 100;

Control Structures

Statements: if, else if, else

Switches

switch(x) {
    case 1: printf("One"); break;
    case 2: printf("Two"); break;
    default: printf("Other");
}

Loops: while, do while, for

For Loop Example

#include <stdio.
...
Continue reading "Essential C Programming Syntax and Memory Management" »

C Ticket Booking System: Linked List Movie Orders in C

Computer science6.01 KB

C Ticket Booking System Using Linked Lists

This program manages movie ticket orders in C using a singly linked list. Pending orders are loaded from a file, new orders can be added, pending tickets can be issued to customers, and all changes are saved back to disk.

Ticket Structure

Each ticket is stored in a struct Ticket with the following fields:

  • ticketID: unique identifier for the order
  • customerID: identifier of the customer
  • movieName: name of the movie
  • seatsNumber: number of seats booked
  • totalPrice: total price of the order
  • next: pointer to the next ticket in the list

Loading Pending Orders

The getPending function reads pendingOrders.txt and builds the linked list one record at a time.

Displaying Pending Orders

The displayPending function prints every...

Continue reading "C Ticket Booking System: Linked List Movie Orders in C" »

Implementing Lexical and Syntax Analysis with Lex and Yacc

Computer science2.73 KB

Lexer Implementation (lexer.l)

%{ #include "y.tab.h" %} %%% "+" { return PLUS; } "-" { return MINUS; } "*" { return MULTIPLY; } "/" { return DIVIDE; } "%" { return MODULUS; } "(" { return LPAREN; } ")" { return RPAREN; } [0-9]+ { yylval.num = atoi(yytext); return NUMBER; } [a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return IDENTIFIER; } [ \t\n] { /* ignore whitespace */ } . { return yytext[0]; } %%%

Parser Implementation (parser.y)

%{ #include <stdio.h> #include <stdlib.h> #include <string.h> void yyerror(const char *s); int yylex(); int temp_count = 0; int label_count = 0; void gen(char op, int arg1, int arg2, int result) { printf("%d: %c, %d, %d, %d\n", label_count++, op, arg1, arg2, result); } int new_temp() { return
...
Continue reading "Implementing Lexical and Syntax Analysis with Lex and Yacc" »

Android Retrofit and Room Tutorial: API, ViewModel, RecyclerView

Computer science18.15 KB

Android Retrofit, ViewModel and RecyclerView Implementation

Retrofit Interface

public interface ApiService {
@GET("posts") // Replace with your API endpoint for posts
Call<List<Post>> getPosts();
}

Post Model Class

public class Post {
private int id;
private String title;
private String body;
}

Retrofit Client

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
private static final String BASE_URL = "https://your-base-url.com/";
private static Retrofit retrofit;

public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(

...
Continue reading "Android Retrofit and Room Tutorial: API, ViewModel, RecyclerView" »

Exploring Basic Linux System Calls

Computer science521.58 KB

Problem Statement

Explore the usage of basic Linux system calls.

Process Management System Calls

The system calls to manage processes are:

  • fork(): Used to create a new process.
  • exec(): Executes a new program.
  • wait(): Waits until the process finishes execution.
  • exit(): Exits from the process.

System calls used to get process IDs are:

  • getpid(): Gets the unique process ID of the process.
  • getppid(): Gets the parent process's unique ID.

Stat System Call

The stat system call retrieves information about files, specifically from their inodes. For instance, to get a file's size and name, you would typically use stat. Let's illustrate with a C code snippet (src/ls1.c):

#include<stdio.h>
#include<dirent.h>
#include<cstdlib>

struct dirent *dptr;
...
Continue reading "Exploring Basic Linux System Calls" »

C++ Object-Oriented Programming Concepts Explained

Computer science2.39 KB

Inheritance

Inheritance is the capability of a class to derive properties and characteristics from another class.

Deep Copy vs. Shallow Copy

Deep Copy

In a deep copy, an object is created by copying the data of all variables and allocating new memory resources with the same values. To perform a deep copy, you must explicitly define a copy constructor and assign dynamic memory if required. This also necessitates dynamic memory allocation in other constructors, requiring custom copy constructors and overloaded assignment operators.

box(box& sample) { 
  length = sample.length; 
  breadth = new int; 
  *breadth = *(sample.breadth); 
  height = sample.height; 
}

Shallow Copy

In a shallow copy, an object is created by simply copying the data of all...

Continue reading "C++ Object-Oriented Programming Concepts Explained" »

Java File I/O, CSV, and Database Demo Tutorial

Computer science6.73 KB

Java File I/O, CSV, and Database Demo

This example demonstrates how to read and write text files, parse CSV data, and perform basic database operations using JDBC with UCanAccess and an MS Access database.

Required Imports

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

Class Declaration and Constants

public class Demo05 {

    private static final String IN_FILE = "./input.txt";
    private static final String OUT_FILE = "./output.txt";
    private static
...
Continue reading "Java File I/O, CSV, and Database Demo Tutorial" »

Java Conditional Logic, Loops & Dues Calculator

Computer science3.27 KB

Java Membership Dues Calculator Example

Below is a corrected and formatted Java example that demonstrates conditionals, a switch, and loops. All original content has been preserved and corrected for spelling, grammar, and capitalization.

import java.util.Scanner;

public class DuesCalculator {
    public static void main(String[] args) {
        // TODO: application logic here
        // 1
        char standing;
        double gpa = 0, dues = 0;
        // 2
        Scanner keyboard = new Scanner(System.in); // import java.util.Scanner

        System.out.print("Enter your class standing: ");
        standing = keyboard.nextLine().charAt(0);

        System.out.println("Enter your GPA:");
        gpa = keyboard.nextDouble();

        // System.
...
Continue reading "Java Conditional Logic, Loops & Dues Calculator" »

Implementing Akka Clusters and Sharding in Lagom

Computer science2.4 KB

Akka Clusters and Sharding with Lagom

This configuration demonstrates how to set up Akka Clusters and Sharding within a Lagom microservices project using Scala.

SBT Configuration

organization in ThisBuild := "com.example"
version in ThisBuild := "1.0-SNAPSHOT"
scalaVersion in ThisBuild := "2.11.8"

val macwire = "com.softwaremill.macwire" %% "macros" % "2.2.5" % "provided"
val scalaTest = "org.scalatest" %% "scalatest" % "3.0.1" % Test

lazy val `lagom-persistent-entity-demo` = (project in file("."))
  .aggregate(`user-api`, `user-impl`)

lazy val `user-api` = (project in file("user-api"))
  .settings(
    libraryDependencies ++= Seq(
      lagomScaladslApi
    )
  )

lazy val `user-impl` = (project in file("user-impl"))
  .enablePlugins(LagomScala)
...
Continue reading "Implementing Akka Clusters and Sharding in Lagom" »