0% found this document useful (0 votes)
5 views3 pages

Week 3 Tutorial 2

Uploaded by

Kang Ming Wong
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)
5 views3 pages

Week 3 Tutorial 2

Uploaded by

Kang Ming Wong
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
You are on page 1/ 3

ECP6223 Algorithms and Data Structures

Tutorial 2
Variables, Pointers, Arrays, Records

1. Determine the output of the program below:


#include <stdio.h>
int main() {
int var=5, *ptr;

ptr = &var;
var = 2*var;

printf("Variable: %p / %d\n", &var, var);


printf("Pointer : %p / %p / %d\n", &ptr, ptr, *ptr);
return 0;
}

2. Consider the following incomplete C program consisting of two user-defined functions,


i.e., printData and findLSE. The program is to input an arbitrary number of employee
records, prints a summary of data received, and then determines the longest serving
employee in the company. Based on the comments provided, complete the code.
#include <stdio.h>
#include <stdlib.h>

//Define a record type called Employee consisting of a character


// array name, integer id and double type of yearService members

void printData(struct Employee *emp, int n);


int findLSE(struct Employee *emp, int n);

int main(){
struct Employee *emps;
int n, i, index;

printf("Number of employee? ");


scanf("%d", &n);

//Allocate sufficient memory space dynamically for storing data


and
// assign the starting address of the allocated space to emps

//Using a for loop,


// input employee information, i.e., name, id and year of service

//Call function printData to print out all data

//Call function findLSE to determine the longest serving employee


// and assign the function-return to index
//Print the name of the longest serving employee

return 0;
}

//Define function printData

//Define function findLSE


_________________________________________________________________________________________
_
NHAA 1 Trimester March 2025
ECP6223 Algorithms and Data Structures

A sample program output is given below. (User inputs are in italic)


Number of employee? 2

Employee name? James Ng


Employee id? 1011
Year of service? 5.6

Employee name? Lily Lee


Employee id? 1021
Year of service? 2.5

Employee name ID Year of Service


James Ng 1011 5.60
Lily Lee 1021 2.60
James Ng is the longest serving employee.

3. Consider the following incomplete program consisting of a function called sum_prod


which receives two integers and two integer pointers as arguments. The function
computes the summation and product of two integers, and stores the results in the pointer
arguments. Based on the comments provided, complete the program.
#include <stdio.h>
//Define function sum_prod

int main() {
int n1, n2, sum, product;

//Prompt user for two numbers


//Input n1 and n2

//Call sum_prod with suitable arguments

printf("%d + %d = %d\n", n1, n2, sum);


printf("%d * %d = %d\n", n1, n2, product);
return 0;
}

_________________________________________________________________________________________
_
NHAA 2 Trimester March 2025
ECP6223 Algorithms and Data Structures

4. Determine the output of the program below:


#include<stdio.h>
int fun (int data1, int *data2);
int main(){
int a=1, b=2, c=3;
printf("a=%d, b=%d, c=%d\n", a, b, c);
a = fun(b,&c);
printf("a=%d, b=%d, c=%d\n", a, b, c);
return 0;
}

int fun (int data1, int *data2){


int result=data1+*data2;
data1=2*data1*(*data2);
*data2=(*data2)*100;
return result;
}

_________________________________________________________________________________________
_
NHAA 3 Trimester March 2025

You might also like