0% found this document useful (0 votes)
3 views

C LAB QUESTIONS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C LAB QUESTIONS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

INTERNATIONAL BUSINESS SCIENCE AND TECHNOLOGY UNIVERSITY

Plot 11A, Rotary Avenue, Lugogo Bypass, Kololo


Ph: +256-41-237525/237526
Fax: +256-41-343776

Student Name: Nyanzi Evalisto


Student Number: 022240835
Course: Bachelor Engineering in Electronic and Communication.
Year: 2025
Semester: 1
Subject Name: Programming in C Practical
Subject Code: ECE1106
Student`s signature:
Lecturer`s name: Kato Kenneth
Lecturer`s signature:
PART 1
NO.1 Understanding Function Syntax.
Define a function in C that calculates the square of a number. Explain the purpose of the
return keyword in the function. Provide an example of how this function is called from the
main () function.
Ans:

The return keyword ends the execution of the function and return a value or signal to the
calling function

NO.2 Function Prototypes C programming


What is a function prototype in c, why is it necessary? Write an example program where
the function prototype is declared above ‘main ()’, but the actual function definition is
placed below ‘main ()’
 A function prototype is a function declaration that provide the function`s name,
return, type, and parameter list without including the function body.
 It`s necessary to inform the compiler about the function`s signature.
NO.3 Passing Arguments by Value. C programing
Explain how arguments are passed to a function by value. Write a function swap (int x, int
y) and call it from main () to demonstrate how the values are swapped when passed by
value.
 Pass a copy of the value in memory. The function operates on a copy. This means
that when a function changes the value, the effect is local to the function. The copy
changes but the original value in memory is not changed.
NO.4 Recursive Functions.
Write a recursive function in C to calculate the factorial of a given number. Explain the
base case and how recursion terminates in the contexts.

 The base case is a fundamental part of recursive function. It dictates where the
recursion ends and begins to return values.
 Recursion terminates when it reaches the base

NO.5 Scope of variables.


Discuss the scope of variables in a function in C. Write the program demonstrating the
difference between the global and the local variable.
 The scope is the area in which a variable can be used or accessed. The are two
scopes of variables in C that is to say local and global variables. A local variable as its
scope to the function only, in which it is declared. Global scope is the entire
program. Global variables can be used anywhere throughout the program.
N0.6 If-else Statements.
Write a C program that take an integer input from the user and checks if it’s a positive,
negative, or zero using if-else statements. Explain how the conditions are evaluated.
 If(num>0) checks if the number is greater than zero. If the condition is true, program
prints positive.
 Else if(num<0) checks if the number is less than zero. If the condition is true, the
program prints negative.
 Else checks if both conditions are false, then the number must be zero

N0.7 Switch Case Statements


Explain how a switch statement work in C. Write a program that uses a ‘switch’ statement
to print the day of the week based on the number entered by the user (1 for Monday, 2
for Tuesday, etc.
A switch statement causes control to transfer to one labelled statement body,
depending on the value of expression. The value of expression and each constant
expression must have an integral type. A constant-expression must have an unambiguous
constant integral value at a compile time
NO.8 Nested If Statement.
Write a program in C that determine whether a student passed or failed based on their
marks. Use a nested if statement to evaluate if the score is >=50 and if it’s a distinction
(>=75)

NO.9 Ternary Operator.


Explain a ternary operator in C and how it can be alternative to if-else statements. Write a
program to find the larger of two numbers using the ternary operator.
 The ternary operator takes three arguments; a condition expression, a result if the
condition is true, and a result if the condition is false. The ternary operator is
represented by “?:” symbol can be used to simplify and make code work more
readable and efficient.
NO.10 Combining Conditions with Logical Operators.
Write a program in C that can take three integers as input and check if all are positive
numbers using a logical operator in an if statement. Explain how the logical AND ( &&)
and logical OR (`ll`) operators are used in the condition statements

 Logic AND returns true only if all values are true for example num1, num2, and num3
are true. If one value is not true, then returns false.
 Logical OR returns true if at least one value is true that is to say check one of the
values is positive.
BOOK QUESTIONS LAB PART 2
1. 1D Array
Write a C program to read n integers into a 1D array, then find and display the largest and
smallest elements. Include a function findMinMax that take the array and its size as
parameters.

2. 2D Array:
Create a C program that reads a 3×3 matrix from the user and computes the sum of each
row and each column. Display the matrix, the row sums, and the column sums.
3. 2D Array:
Write a c program to perform matrix multiplication for two matrices provided by the user.
Ensure that the matrices dimensions are compatible and display the resulting matrix.
4. 3D Array:
Write a C program to store and display sales data for 3 products across 4 regions for two
quarters. Use a 3D array, and calculate the total sales for each product across all regions
and quarters.
5. Mixed Array Operation:
Explain the advantages of using Array for storing data.
 Efficient and Fast Access. Arrays allow direct and efficient access to any element in
the collection with constant access time, as the data is stored in contiguous memory
locations.
 Memory Efficiency. Arrays store elements in contiguous memory, allowing efficient
allocation in a single block and reducing memory fragmentation.
 Versatility: Arrays can be used to store a wide range of data types, including integers,
characters, floating-point number, and even complex data structures such as objects
and pointers.
 Compatibility with hardware: The array data structure is compatible with most
hardware architectures, which make it a versatile tool for programming in a wide
range of environments.
NO.1 Union:
Write a program to define a union `Date` with fields for an integer, a float, and a
character. Demonstrate how modifying one field affects the other fields, explain the
behaviour observed.

2. Structure:
Create a structure Student with fields for name, roll number, and marks in three subjects.
Write a program to read and display the details of n students and compute their average
marks.
3. Structure with Nested Structures:
Define a structure Employee with fields for name, ID, and a nested structure Address that
includes city, state, and zip code. Write a program to input and display the details of an
employee.
4. Union vs. Structure:
Explain the differences between Unions and Structures
A structure is a custom data type that holds multiple members of different data type under
a single unit where union is a user defined data type that combine object of different data
type in the exact memory location. Differences include:
 Memory Allocation. Structure allocates memory for all its members while union
allocates memory only for the largest member.
 Total Size. Structure shows sum of sizes of all members whereas union shows size of
the largest member.
 Data Storage. Structure can store values for all members simultaneously while union
can store value for only one member at a time.
 Accessing Members. In structure, all members can be accessed at any time while in
union only the last stored member can be accessed.
 Modification Impact. Modifying a member in structure doesn’t affect other members
whereas in union modifying one member may overwrite other members.

5. Complex Data Operations:


Define a structure `Book` with fields for title, author, price, and publication year. Create
an array of `Book` structures to manage a small library system, allowing the user to search
for books by author or display all books published after a given year.
END

You might also like