2023 - Lab Sheet #6 - ECEN1012

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ECEN1012: COMPUTER PROGRAMMING AND NETWORK FUNDAMENTALS

LAB #6

NAME:____________________
STUDENT ID:____________________
MARK:____________________

Due date: November 30, 2023

Note: If any kind of plagiarism is found, the involved students will receive zero marks for

this assignment.
Question 6-1: Please compile and execute the following C program, understand the output, then copy
and paste the output.

Code:

#include <stdio.h>
int main(void)
{
int i=5, m=300;
float fx = 300.60;
char cht = 'z';
int *pt1, *p;
float *pt2;
char *pt3;
p=&i;
pt1= &m;
pt2=&fx;
pt3=&cht;
printf("%d\n %d\n %d\n %d\n", *p+2, **&p, 3**p, **&p+4);
printf ( " m = %d\n",m);
printf ( " fx = %f\n",fx);
printf ( " cht = %c\n",cht);
printf("-----------------------\n");
printf ( " address of m = %p\n",&m);
printf ( " address of fx = %p\n",&fx);
printf ( " address of cht = %p\n",&cht);
printf("-----------------------------\n");
printf ( " value at address of m = %d\n",*(&m));
printf ( " value at address of fx = %f\n",*(&fx));
printf ( " value at address of cht = %c\n",*(&cht));
printf("----------------------------------\n");
printf ( " address of m = %p\n",pt1);
printf ( " address of fx = %p\n",pt2);
printf ( " address of cht = %p\n",pt3);
printf("----------------------------------\n");
printf ( " value at address of m = %d\n",*pt1);
printf ( " value at address of fx= %f\n",*pt2);
printf ( " value at address of cht= %c\n\n",*pt3);
system("pause");
return(0);
}

Please copy and paste the output:


Question 6-2 : Please complete the following C program to swap elements using call by reference. For
example, when the three elements we input from the keyboard is 30, 60, and 90, respectively. The
output of the program is given as follows:

Input the value of 1st element: 30


Input the value of 2st element: 60
Input the value of 3rd element: 90

The value before swapping are :


element 1 = 30
element 2 = 60
element 3 = 90

The value after swapping are :


element 1 = 90
element 2 = 30
element 3 = 60

Code
#include <stdio.h>
void swapNumbers(int *x,int *y,int *z);
int main()
{
int e1,e2,e3;
printf(" Input the value of 1st element : ");
scanf("%d",&e1);
printf(" Input the value of 2nd element : ");
scanf("%d",&e2);
printf(" Input the value of 3rd element : ");
scanf("%d",&e3);
printf("\n The value before swapping are :\n");
printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n",e1,e2,e3);
swapNumbers(&e1,&e2,&e3);
printf("\n The value after swapping are :\n");
printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n\n",e1,e2,e3);
return 0;
}
Void swapnumbers(int *a,int *b, int *c);
{int *a, *b, *c, unio;
Unio=a;
A=c;
C=unio;)
Please complete the code for function swapNumbers

Please copy and paste the output:

Question 6-3: Please write a C program to add two number using pointers. For example, when the
input from the keyboard are 5 and 6, the output of the program is given as follows:

Input the first number: 5


Input the second number: 6
The sum is: 11

Code:
#include <stdio.h>
Int swap(int *a,int *b)
void main()
{
int fno, sno, *ptr, *qtr, sum;
printf("Input the first number: \n",n);
scanf("%d", &fno);
printf("Input the second number: \n",n);
scanf("%d", &sno);
swap

Please complete this part of code

system("pause");
return(0);
}
Please copy and paste the output:

Question 6-4: Please write a C program to find the factorial of a given number using pointers, where the
number is input from the keyboard.

Code:

Please complete the code

Please copy and paste the output:

You might also like