[go: up one dir, main page]

0% found this document useful (0 votes)
23 views8 pages

C Programming Assignment Overview

The document outlines the assignment details for ELEC2030: Computer System Programming at VinUniversity for Spring 2025, focusing on C programming concepts such as loops, recursion, pointers, and arrays. It includes specific tasks for students to complete, submission guidelines, learning outcomes, and references. The assignment consists of multiple programming problems that require implementing algorithms, data structures, and using various programming techniques in C.

Uploaded by

wuhuanglanii8506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

C Programming Assignment Overview

The document outlines the assignment details for ELEC2030: Computer System Programming at VinUniversity for Spring 2025, focusing on C programming concepts such as loops, recursion, pointers, and arrays. It includes specific tasks for students to complete, submission guidelines, learning outcomes, and references. The assignment consists of multiple programming problems that require implementing algorithms, data structures, and using various programming techniques in C.

Uploaded by

wuhuanglanii8506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ELEC2030: SYSTEM PROGRAMMING

VinUniversity
College of Engineering and Computer
Science

ELEC2030 – COMPUTER SYSTEM PROGRAMMING - SPRING 2025


ASSIGNMENT #1 – Introduction to C, Loops, Recursion, Types,
Pointers, and Arrays
Do Danh Cuong & Vo Phi Son
E-mail: {[Link], [Link] }@[Link]
Time and date: 13:30, 4th March. 2025
Report due: 23:30, 11th March.
2025
Report notes
Key requirements of your report:
 You are expected to complete all tasks and answer all questions (if
required). Please include the result figures (if required) and your
comments, style must follow: Google C++ Style Guide
 Homework’s information (Title, Date, Student’s Name, Instructor’s Name)
must be included.
 Answers must be in the order assigned and written in English.
 Late submission policy: 5 points will be deducted for each hour of late
submission.

Learning outcomes
 Demonstrate the ability to effectively use the C and C++ programming
languages to solve small programming problems.
 Describe both basic and advanced algorithms and data structures, how to
implement these algorithms and data structures in C/C++

References
 The C programming language, Dennis Ritchie, Brian Kernighan, 2nd
Ed.
 All of Programming, A. Hilton and A. Bracy, 2015.

1. Lab Practice Submission Instructions:


This is an individual lab practice and will typically be assigned in the
computer laboratory.

1
ELEC2030: SYSTEM PROGRAMMING

• Your program should work correctly on all inputs. If there are any
specifications about how the program should be written (or how the
output should appear), those specifications should be followed.
• Your code and functions/modules should be appropriately commented.
However, try to avoid making your code overly busy (e.g., include a
comment on every line).
• Variables and functions should have meaningful names, and code should
be organized into functions/methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You
should NOT copy or share your code with other students to avoid
plagiarism issues.
• Use the template provided to prepare your solutions (modified to
provide your student ID, your name, etc).
• You should upload your solutions to the Canvas
• Submit a separate file for each lab problem with the exact file name
specified in each lab question.
• Late submission of lab practice without an approved extension will incur
the following penalties. Late submission will incur 5 points will be
deducted for each hour of late submission.

2. Assignment 01
2.1. Introduction to C (20 points)
a) Write a program titled assg01_p01.c that does the following: Given a
non-negative integer N ≥1 . The output must be as table 1.

Input a non-negative integer N ≥1 .


Output: Print out produces sequences of integers as outputs.
Example:
Input (N) Output
0 N/A
1 1
2 4→2→1
3 9 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40
→ 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
4 16 → 8 → 4 → 2 → 1
5 25 → 76 → 38 → 19 → 58 → 29 → 88 → 44 → 22 → 11 → 34 →
17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 →
1
b) You should explain your solution (how this algorithm works).
2.2. Loops (20 points)
Write a program titled assg02_p02.c that implements two functions:
a) Function 1: Prime Factorization with Advanced Properties
2
ELEC2030: SYSTEM PROGRAMMING

void printAdvancedFactors(int n);


This function takes an integer N and performs the following:
 Prime Factorization: Prints the prime factorization of NNN in the
format "p1 * p2 * ... * pk".
 Sum of Unique Prime Factors: Prints the sum of unique prime
factors.
 Prime Exponent Representation: Prints the prime factorization in
exponent format (e.g., 2^2 * 3^2 * 5^1).
 If N≤1N \leq 1N≤1, print nothing.
b) Function 2: Counting Numbers with Exactly Two Distinct Prime
Factors
int countTwoPrimeFactorNumbers(int N);

Counts and returns how many numbers from 1 to 𝑁 have exactly two
This function:

distinct prime factors (e.g., 6 = 2 * 3, 10 = 2 * 5, 15 = 3 * 5).

Input: A positive integer N (1 < N < 2 × 109)


Output:
For Function 1:
 Prime factorization: 2 * 2 * 3 * 3 * 5
 Sum of unique prime factors: 10
 Exponent representation: 2^2 * 3^2 * 5^1
For Function 2:
 Count of numbers with exactly two distinct prime factors: X

*Requirements:
1. Efficient Prime Factorization: Use loops (for, while) to compute
factorization in O(√N) complexity.
2. Handling Large Inputs: Use long long int if needed for large
numbers.
3. Tracking Unique Factors: Use arrays or hash maps to store
unique primes and their counts.
4. Optimized Counting Algorithm: The function
countTwoPrimeFactorNumbers(N) must efficiently determine
numbers with exactly two distinct prime factors.
5. Edge Cases: Handle large primes and cases where NNN is very
small.

Example:
Input Output
180 2*2*3*3*5
10
2^2 * 3^2 * 5^1

3
ELEC2030: SYSTEM PROGRAMMING

132 2 * 2 * 3 * 11
16
2^2 * 3^1 * 11^1

2.3. Struct Type (20 points)


Declare a rectangular prism class/data structure with lengths of 3 sides
(integer type).
Write a program titled assg01_p03.c that takes two integers that are the
length and width, high of a rectangular prism and calculates the lateral
surface area and total surface area of that rectangular prism.
Input: A single line from the keyboard containing 3 positive integers with
a value not exceeding 100, separated by a space, representing the length
and width of a rectangle.
Output: Print to the screen a single line containing 2 integers that are the
lateral surface area and total surface area of that rectangular prism.
Your task is to complete the ’//your code goes here’ section of the sample
code below.
#include <stdio.h>

// Define a rectangular prism structure


struct RectangularPrism {
int length;
int width;
int height;
};

// Function to calculate lateral surface area


int calculateLateralSurfaceArea(struct RectangularPrism prism) {
// write your code here
}

// Function to calculate total surface area


int calculateTotalSurfaceArea(struct RectangularPrism prism) {
// write your code here
}

int main() {
// Declare a rectangular prism variable
struct RectangularPrism prism;

// Get input from the user


printf("Enter the length, width, and height of the rectangular prism (separated by
spaces): ");

4
ELEC2030: SYSTEM PROGRAMMING

// write your code here


// Check for valid input
if ( // write your code here) {
printf("Invalid input. Please enter positive integers not exceeding 100.\n");
return 1; // Exit with an error code
}

// Calculate and print the lateral and total surface areas


// write your code here
int lateralSurfaceArea =
int totalSurfaceArea =

printf("Lateral Surface Area: %d\n", lateralSurfaceArea);


printf("Total Surface Area: %d\n", totalSurfaceArea);

return 0; // Exit successfully


}

Example:
Input Output
123 Lateral Surface Area: 18
Total Surface Area: 22
4 5 -6 Invalid input. Please enter
positive integers not exceeding
100.

Question 2.3: What is the size of prism? Explain how you can calculate in
more detail?
Your answer:

2.4. Pointer Arithmetic and Function Pointers (20


points).
Write a program titled assg01_p04.c that performs various operations
using pointer arithmetic and function pointers. Your task is to complete
the missing parts of the program to achieve the desired functionality.
Requirements:
1. Implement the missing functions to perform the following
operations:
 ‘add’: Accepts two integer pointers as arguments and returns
the sum of the values they point to.

5
ELEC2030: SYSTEM PROGRAMMING

 ‘subtract’: Accepts two integer pointers as arguments and


returns the result of subtracting the second value from the first
value.
 ‘multiply’: Accepts two integer pointers as arguments and
returns the product of the values they point to.
2. Define a function pointer type named ‘Operation’ that can point to
functions taking two integer pointers as arguments and returning an
integer.
3. Implement a function named ‘performOperation’ that accepts two
integer pointers and a function pointer of type ‘Operation’. It
should call the function pointed to by the function pointer and return
the result.
4. Test your implementations by calling the ‘performOperation’
function with sample inputs and printing the results.
Constraints:
 Use pointer arithmetic to access and manipulate the values.
 Implement the functions in such a way that they operate directly
on the values pointed to by the pointers.
 Handle edge cases appropriately, such as division by zero.
Output: Print out the value of:
 Sum of a and b
 Difference between a and b (with a – b)
 Product: the value of a * b
Your task is to complete the ’//your code goes here’ section of the sample
code below.
#include <stdio.h>

// Function to add two integer values


int add(int *a, int *b) {
// Your code here
}

// Function to subtract the second value from the first value


int subtract(int *a, int *b) {
// Your code here
}

// Function to multiply two integer values


int multiply(int *a, int *b) {
// Your code here
}

// Define function pointer type


typedef int (*Operation)(int *, int *);

6
ELEC2030: SYSTEM PROGRAMMING

// Function to perform an operation using function pointers


int performOperation(int *a, int *b, Operation op) {
// Your code here
}

int main() {
int a = 20, b = 10;

// Test add function


printf("Sum: %d\n", performOperation(&a, &b, add));

// Test subtract function


printf("Difference: %d\n", performOperation(&a, &b, subtract));

// Test multiply function


printf("Product: %d\n", performOperation(&a, &b, multiply));

return 0;
}

Example:
Input Output
a = 20; b = 10 Sum: 30
Difference: 10
Product: 200

2.4. Header File Integration (20 points).


Description:
You are tasked with creating a simple program that involves multiple
source files and a header file. The goal is to practice using header files to
declare functions and structures and include them in multiple source files.

Requirements:

1 Create a header file named ‘operations.h' that contains the


following:
 Declaration of a structure ‘Rectangle’ with two integer members
(‘length’ and ‘width’).
 Function prototypes for the following operations:
 ‘int calculateArea(const struct Rectangle *rect);’ -
calculates and returns the area of the rectangle.
 ‘int calculatePerimeter(const struct Rectangle *rect);’
- calculates and returns the perimeter of the rectangle.

7
ELEC2030: SYSTEM PROGRAMMING

2 Implement the functions declared in the ‘operations.h’ header file


in a source file named ‘operations.c’.
3 Create another source file named ‘main.c’ that includes the
‘operations.h’ header file.
4 In ‘main.c’, prompt the user to enter the length and width of a
‘rectangle’, create a Rectangle structure, and use the functions
from ‘operations.c’ to calculate and display the area and
perimeter of the rectangle.
5 Compile and link the source files to create the executable.
Constraints:

 The length and width of the rectangle should be positive integers.


 Use proper error handling to ensure valid input.
 The program should gracefully handle any errors and not crash.
Example:
Input Output
Enter the length of the rectangle: 3 Rectangle Information:
Enter the width of the rectangle: 2 Area: 6
Perimeter: 10

You might also like