0% found this document useful (0 votes)
41 views15 pages

PL 1 Sheets

The document contains a series of programming exercises for a CS112 course at Helwan University, focusing on C programming basics, functions, arrays, strings, structures, and pointers. Each exercise includes specific tasks such as writing programs to perform calculations, manipulate data, and implement algorithms using various programming concepts. The exercises are divided into multiple sheets, each addressing different topics and programming skills for students to practice.

Uploaded by

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

PL 1 Sheets

The document contains a series of programming exercises for a CS112 course at Helwan University, focusing on C programming basics, functions, arrays, strings, structures, and pointers. Each exercise includes specific tasks such as writing programs to perform calculations, manipulate data, and implement algorithms using various programming concepts. The exercises are divided into multiple sheets, each addressing different topics and programming skills for students to practice.

Uploaded by

nassar0727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department

CS112 Programming Languages 1


Spring 2024

Exercises - Problems Sheet # 0: Revision on C Basics Programming


Spring 2024

No. Of Questions: 15 No. Of Pages: 2

Answer the following:


1) Write a C program to print the following line as shown below:
Welcome!
You are able to test your skill of writing C code here.

2) Write a C program to declare two integer and one float variables, then initialize them to 10, 15,
and 12.6. It then prints these values on the screen.

3) Write a C program to prompt the user (i.e. ask the user) to input 3 integer values and print these
values in the reversed order.

4) Given the following pseudo code, write a program that executes it.
a. read x
b. read y
c. compute p = x y
d. compute s = x + y
e. total = s2 + p ( s – x ) ( p + y )
f. print total

5) Write a C program that finds the area of a triangle. Note: a triangle’s area = 0.5 * base * height

6) Write a C program that prompts the user to input three integer values and find the greatest value
of those three values.

7) Write a program that determines a student’s grade. The program will read three scores and
determine the grade based on the following rules:
● if the average score >= 90% => grade = A
● if the average score >= 70% and < 90% => grade = B
● if the average score >= 50% and < 70% => grade = C
● if the average score < 50% => grade = F

8) Write a C program that finds whether a number is even or odd.

9) Write a C program that compares two numbers a and b. The output of this comparison is whether
the two numbers are equal, that a is greater, or that b is greater.

10) Write a C program that finds the type of a triangle when its three angles are given.
● If all angles are equal, it is an equilateral triangle.
● If any two angles are equal, it is an isosceles triangle.
2 /1

CS112 Exercises – Problems Sheet # 0


● If all angles are different, it is an acute triangle.

11) Write a C program that prompts the user to choose an operation to do on 2 input integers. The
operations are addition, subtraction, multiplication, and division. Note: the program should not allow
the division by zero.

12) Write a C program that will print the following pattern:


*******
******
*****
****
***
**
*

13) Write a C program that will print the following pattern:


*
***
*****
*******
*********
*********
*******
*****
***
*

14) Write a C program that finds the sum of the first n natural numbers.

15) Write a C code that finds the sum of the digits of a number.

With our best wishes;

2 /2

CS112 Exercises – Problems Sheet # 0


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 1: Functions (Recursion)


Spring 2024
No. Of Questions: 7 No. Of Pages: 2

Answer the following:


1) Repeat the problem no. 11 in Sheet#0 but with an ‘Exit’ option; that is, the program allows the
user to enter new values and choose the required operations until the user chooses to exit.

2) Repeat the problems from 5 to 16 in Sheet#0 using functions, and you should call them from the
main. The functions should return a value to the main and the main is responsible for printing the
output.

3) Write a program that will:


● Prompt the user to input ten integer values.
● Calculate the smallest and the greatest of those values.
● Call a function to calculate the difference between those smallest and greatest values.

4) Write a C program to find the sum of the first n natural numbers using recursion.
Note: Positive integers are known as natural numbers.

5) Write a C program to check whether a number is a prime number or not, by using recursion.

6) Write a C program to reverse a string, by using recursion.

2 /1

CS112 Exercises – Problems Sheet # 1


7) What does the following program do?

With our best wishes;

2 /2

CS112 Exercises – Problems Sheet # 1


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 2: One/Two Dimensional Arrays


Spring 2024

No. Of Questions: 14 No. Of Pages: 2

Answer the following:


1) Write a C program that calculates the maximum, the minimum, and the average of 10 numbers
entered by the user and stored in an array.

2) Write a C function that checks if an array is in an ascending order.

3) Write a C program that reads 10 integer numbers from the user, and then the program should
calculate the sum of the odd numbers, and the sum of the even numbers.

4) Write a C program that asks the user to enter 10 integers in an array. The program will then
display (based on the entered numbers) one of the following messages:
● "the numbers in the array are increasing",
● "the numbers in the array are decreasing",
● "the numbers in the array are not changing", or
● "the numbers in the array are increasing and then decreasing."

5) Write a C program that reads a matrix (3🞨4), and asks the user to choose a number, and then
displays the position of the selected number if found, otherwise it displays “number not found.”

6) Write a C program to read a matrix from the user, and then display the row with the maximum
total/sum (that is, the row whose sum of elements is maximum).

7) By using a two-dimensional array, write a C program to display the matrix shown below:

0 1 1 1 1

-1 0 1 1 1

-1 -1 0 1 1

-1 -1 -1 0 1

-1 -1 -1 -1 0

8) By using a two-dimensional array, write a C program to display a Pascal triangle of any size. In a
Pascal triangle, the first & second rows are set to 1. Each element of the triangle (starting from the
third row downwards) is the sum of the element directly above it and the element to the left of the
element directly above it. See the following example of a Pascal triangle (with a size = 5):
1

2/1

CS112 Exercises – Problems Sheet # 2


1 1
1 2 1
1 3 3 1
1 4 6 4 1

9) Write a C function that reverses an array in another one.

10) Write a C function that reads the number of students in a class, and five grades for each student.
The function should then compute the average grade for each student.

11) Write a C function that checks if a matrix is sparse or not. Given that a sparse matrix is a matrix
in which most of the elements are zero (that is, the number of zero-valued elements are more than
50% of the total number of elements).

12) Write a C function that reads a matrix and checks whether the given matrix is a symmetric
matrix or not. Given that: If a square matrix A is equal to its transpose AT, then it is a symmetric
matrix. For example: if the elements of the matrix are:
1 2 3
2 4 5
3 5 8
Then the matrix is symmetric

13) Write a C program that reads the radius of a circle, and then calls a function that returns the
circumference and the area of that circle. The program should include a global constant variable.

14) Write a C program that reads a 1-D array of any size, then calls a function that returns the
following:
● The maximum value in the array.
● The minimum value in the array.
● The average value of the array.

With our best wishes;

2/2

CS112 Exercises – Problems Sheet # 2


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 3: String, Structures, and Enumeration


Spring 2024

No. Of Questions: 9 No. Of Pages: 2

Answer the following:


1) Write a C function that concatenates two input strings S1 and S2 in string S1.

2) Write a C program that reads 10 characters from the user, and then searches for the position of
the character z.

3) Write a C program that reads string S1 and certain letter from the user, then call your own
function that return the number of occurrences of the given character in the given string.

4) Write a C function that take two strings (array of characters) and return one if the 1st is part of
the 2nd and zero otherwise

5) Write a C code to reverse a string by recursion.

6) Write a function ``replace'' which takes a string as a parameter and replaces all spaces in that
string by minus signs and delivers the number of spaces it replaced.

7) What is the output of these codes?


a) int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}

b) int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU,
FRI, SAT);
return 0;
}

2 /1

CS112 Exercises – Problems Sheet # 3


8) Declare a structure Employee with id_no, salary, birth_date – which is day, month, and year- id
for 5 tasks the employee has. For example the data for an employee may be: id_no = 5, salary =
7500, birth_date = { day= 3, month= 8, year = 1980}, tasks_ids = {1, 3, 4, 9, 12}.

a) For the previous declaration, write a function which input one employee data.

b) For the previous declaration, write a function which take an array of employee’s data and an
id_no, the function should search for that employee in the employees array and return his salary, or
return -1 if the employee id not found.

c) Use all the previous in a program that enter data for 5 employees, then the program takes one
id_no for an employee, search for it, if found return his salary, otherwise write "NOT FOUND".

9) Show the output:

main() {
struct s {
double x;
int y;
} a_struct;
printf("The size of a_struct: %d-byte\n",
sizeof(a_struct));
}

With our best wishes;

2 /2

CS112 Exercises – Problems Sheet # 3


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 4: Pointers


Spring 2024

No. Of Questions: 4 No. Of Pages: 1

Answer the following (using pointer/offset notation):


1) Write a C program that calculates the maximum, the minimum, and the average of 10 numbers
entered by the user and stored in an array.

2) Write a C program that reads 10 characters from the user, and then searches for the position of the
character z.

3) Write a C function that checks if an array is in an ascending order.

4) Write a C program that reads 10 integer numbers from the user, and then the program should
calculate the sum of the odd numbers, and the sum of the even numbers.

With our best wishes;

1/1

CS112 Exercises – Problems Sheet # 4


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 5: Pointers


Spring 2024

No. Of Questions: 2 No. Of Pages: 1

Answer the following (using pointer/offset notation):


1) Write a C program that asks the user to enter 10 integers in an array. The program will then
display (based on the entered numbers) one of the following messages:
● "the numbers in the array are increasing",
● "the numbers in the array are decreasing",
● "the numbers in the array are not changing", or
● "the numbers in the array are increasing and then decreasing."

2) Trace the following:


A) B)
int main(void) int main()
{ {
char ch = 'c'; int array[10] = { 2, 5, 9, 0, 3, 7 , 2};
char *chptr = &ch; int *data_ptr;
int i = 20; int value;
int *intptr = &i; data_ptr = &array[0];
float f = 1.20000; value = *data_ptr++;
float *fptr = &f; printf("%d\n", value);
char *ptr = "I am a string"; value = *++data_ptr;
printf("\n [%c], [%d], [%f], [%c], [%s]\n", printf("%d\n", value);
*chptr, *intptr, *fptr, *ptr, ptr); value = ++*data_ptr;
return 0; printf("%d\n", value);
} value = *data_ptr;
printf("%d\n", value);
}

With our best wishes;

1/1

CS112 Exercises – Problems Sheet # 5


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 6:


Pointers, Strings, & Unions
Spring 2024

No. Of Questions: 14 No. Of Pages: 4

Answer the following:


1) Write a short program that declares an integer variable and initializes its value to 25. Then declare
a pointer to that variable. Print the value by using that pointer.

2) Write a program similar to the previous one, but instead of an integer, use a string initialized with
your own name.

3) Write a program that:

• For a trip, prompts the user for the number of driven miles, and the fuel consumption.
• Calls a function with three parameters: (1) the number of miles, (2) the fuel consumption,
and (3) the fuel consumption per mile - which is calculated within the function.
• Prints the fuel consumption per mile – which is calculated within the function.

4) Write a program that reads 5 integers to an array. The integers should then be printed. Use pointer
arithmetic.

5) Modify the preceding program so that it would calculate the sum of the integers. Use a
pointer variable for the sum.

6) Write a function which takes as arguments: (1) a pointer to an integer array, and (2) the number of
items of the array. The function then finds and return the greatest item. In the main function, the user
should enter 8 numbers to be stored in the array. The function is then called, and the returned
greatest item is printed (in the main function).

7) Write a recursive function that returns the length of a string.

8) Write a recursive function that returns the number of spaces in a string.

9) Write a recursive function that checks if a string is entirely consisting of digits, or not.

10) Write a program that asks the user to enter his/her name and age. Then asks the user whether
(s)he is a U.S. citizen. If the answer is Yes, ask the user to enter the name of the state where (s)he
comes from. Otherwise, ask the user to enter the name of the country (s)he comes from. (You're
required to use a struct and a union in your program).

4 /1

CS112 Exercises – Problems Sheet # 6


11) [From the textbook "C How to program": 10.21] In this exercise, you’ll design a "starter"
HealthProfile structure for a person. The structure’s members should include: the person’s first
name, last name, gender, date of birth (consisting of separate attributes for the month, day and year
of birth), height, and weight. Your program should have a function that:
● Receives this data and uses it to set the members of a HealthProfile variable.
● Calculates and returns the user’s age in years.
● Calculates the Body-Mass-Index (BMI). (BMI = weight / ( height * height ) )
● Note: The program should prompt for the person’s information, create a HealthProfile
variable for that person, and display the information from that variable — including the person’s
first name, last name, gender, date of birth, height and weight, then it should calculate and display
the person’s age in years, and his/her BMI.

12) Trace the following:

A B
void swap_nums( int *x, int *y ) { #include <stdio.h>
int tmp; int add_two( int x, int y )
tmp = *x; {
*x = *y; static int counter = 1;
*y = tmp; printf("This is the function call of %d,\n",
} counter++);
return ( x + y );
void swap_pointers( char *x, char *y ) { }
char *tmp;
tmp = x; /* the main function */
x = y; main()
y = tmp; {
} int i, j;
for ( i = 0, j = 5; i < 5; i++, j-- )
int main() { printf( "the addition of %d and %d is
int a,b; char *s1,*s2; %d.\n\n", i, j, add_two( i, j ) );
a = 3; b=4; return 0;
swap_nums( &a, &b ); }
printf( "a is %d\n", a);
printf( "b is %d\n", b);
s1 = "I should print second";
s2 = "I should print first";
swap_pointers( s1, s2 );
printf( "s1 is %s\n", s1 );
printf( "s2 is %s\n", s2 );
return 0;
}

13) Show the output of the following program:


main () {
4 /2

CS112 Exercises – Problems Sheet # 6


union u {
double x;
int y;
} a_union;
struct s {
double x;
int y;
} a_struct;
printf("The size of a_union: %d-byte\n", sizeof(a_union));
printf("The size of a_struct: %d-byte\n", sizeof(a_struct));
}

14) Trace the following program:


#define FLOAT_TYPE 1
#define CHAR_TYPE 2
#define INT_TYPE 3

struct var_type {
int type_in_union;
union {
float un_float;
char un_char;
int un_int;
} vt_un;
} var_type;

void print_vt( void ) {


switch( var_type.type_in_union ) {
default:
printf("Unknown type in union\n");
break;
case FLOAT_TYPE:
printf("%f\n", var_type.vt_un.un_float);
break;
case CHAR_TYPE:
printf("%c\n", var_type.vt_un.un_char);
break;
case INT_TYPE:
printf("%d\n", var_type.vt_un.un_int);
break;
}

4 /3

CS112 Exercises – Problems Sheet # 6


}

main() {
var_type.type_in_union = FLOAT_TYPE;
var_type.vt_un.un_float = 3.5;
print_vt();
var_type.type_in_union = CHAR_TYPE;
var_type.vt_un.un_char = 'a';
print_vt();
}

With our best wishes;

4 /4

CS112 Exercises – Problems Sheet # 6


Helwan University – Faculty of Computers and Artificial Intelligence (FCAI) – Computer Science Department
CS112 Programming Languages 1

Exercises - Problems Sheet # 7:


Search & Sort Algorithms
Spring 2024
No. Of Questions: 5 No. Of Pages: 1

Answer the following:


1. Write a function that takes an array of integers and tells whether it is sorted or not.
2. Modify the bubble sort function to check at the end of each pass whether any swaps have
been made. If none has been made, then the data must already be in the proper order, so the function
should terminate. If swaps have been made, then at least one more pass is needed.
3. Rewrite the linear search algorithm recursively.
4. Create a text-based, menu-driven program that allows the user to choose whether to calculate
the mean, the mode, or the median of an array. The program should then perform the appropriate
calculation and display the result.
5. Write a function "replace" which takes a string as a parameter. It should replace all the spaces
in that string by minus signs, and then counts and prints the number of spaces it replaced.

With our best wishes;

1/1

CS112 Exercises – Problems Sheet # 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy