0% found this document useful (0 votes)
27 views4 pages

Lab 2

The document contains 4 exercises from a CS lab. Exercise I demonstrates pointer operations on variables A, B, and C. Exercise II sorts an array of names using pointers and string functions. Exercise III merges two integer arrays A and B into a single array using pointers. Exercise IV reverses an integer array using pointers by swapping the first and last elements, second and second to last, and so on.

Uploaded by

Dima Azzam
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)
27 views4 pages

Lab 2

The document contains 4 exercises from a CS lab. Exercise I demonstrates pointer operations on variables A, B, and C. Exercise II sorts an array of names using pointers and string functions. Exercise III merges two integer arrays A and B into a single array using pointers. Exercise IV reverses an integer array using pointers by swapping the first and last elements, second and second to last, and so on.

Uploaded by

Dima Azzam
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/ 4

CS Lab #2

Dima Azzam 6302

Mariam Houmani 6316

Mohammad Chouaib 6523

Ex. I

A B C P1 P2
initial 1 2 3 ? ?
P1=&A 1 2 3 &A ?
P2=&C 1 2 3 &A &C
*P1=(*P2)++ 3 2 4 &A &C
P1=P2 3 2 4 &C &C
P2=&B 3 2 4 &C &B
*P1-=*P2 3 2 2 &C &B
++*P2 3 3 2 &C &B
*P1*=*P2 3 3 6 &C &B
A=++*P2* *P1 24 4 6 &C &B
P1=&A 24 4 6 &A &B
*P2=*P1/=*P2 6 6 6 &A &B

Ex.II

#include<stdio.h>
#include <string.h>
#include <malloc.h>
void print(char **names)
{
for(int i=0;i<5;i++)
puts(*(names+i));
}
void arrange()
{
char **names=NULL,*temp=NULL;
names=(char **)malloc(5*sizeof(char *));
temp=(char *)malloc(200*sizeof(char));
for(int i=0;i<5;i++)
*(names+i)=(char *)malloc(200*sizeof(char));
for(int i=0;i<5;i++)
{
printf("Name #%d: ",i+1);
gets(*(names+i));
}
for(int i=0;i<4;i++)
for(int j=i+1;j<5;j++)
{
if(strcmp(*(names+j),*(names+i))<0)
{
strcpy(temp,*(names+i));
strcpy(*(names+i),*(names+j));
strcpy(*(names+j),temp);
}
}
print(names);
for(int i=0;i<5;i++)
free(*(names+i));
free(names);
}
int main()
{
arrange();
}

Ex.III

#include<stdio.h>
#include <malloc.h>
void merge()
{
int *A=NULL,*B=NULL;
int N,M;
int *P1,*P2;
do{
printf("N= ");
scanf("%d",&N);
}while(N<=0);
A=(int *)malloc((N+M)*sizeof(int));
for(int i=0;i<N;i++)
{
printf("A[%d]= ",i+1);
scanf("%d",A+i);
}
P1=A+N;
do{
printf("M= ");
scanf("%d",&M);
}while(M<=0);
B=(int *)malloc(M*sizeof(int));
for(int i=0;i<M;i++)
{
printf("B[%d]= ",i+1);
scanf("%d",B+i);
}
P2=B;
for(int j=0;j<M;j++)
{
*P1=*P2;
P1++;P2++;
}
for(int i=0;i<N+M;i++)
printf("%d ",*(A+i));
free(A);
free(B);
}
int main()
{
merge();
}

Ex.IV

#include<stdio.h>
void reverse(int *A)
{
int *p1,*p2;
int AIDE;
p1=A;
p2=A+size-1;
while(p1!=p2)
{
AIDE=*p1;
*p1=*p2;
*p2=AIDE;
p2--;p1++;
}
}
int main()
{
int *a;
a=(int *)malloc(5*sizeof(int));
for(int i=0;i<5;i++)
scanf("%d",a+i);
reverse(a);
for(int i=0;i<5;i++)
printf("%d ",*(a+i));
free(a);
}

You might also like