You are on page 1of 14

Shiv Nadar University Chennai

School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

Date:17/12/23
Name: Kanchana A
Roll No: 23110548
A1: Using Pointers in C

Aim: Solve the following problems using pointers in C

Question 1: Do Matrix multiplication for two matrices with dynamic size. Use
pointers and functions

Code:
#include <stdio.h>
int matmult( int *x,int *y,int r1,int r2,int c1,int c2)
{
int i,j,k,sum=0;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
sum=sum+(*(x+i*c1+k)*(*(y+k*c1+j)));

}
printf("%d ",sum);
sum=0;
}
printf("\n");
}

int main() {
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
int m[10][10],n[10][10];
int *x=&m[0][0],*y=&n[0][0];
int i,k,j,r1,r2,c1,c2,sum=0;
printf("enter r1:");
scanf("%d",&r1);
printf("enter r2:");
scanf("%d",&r2);
printf("enter c1:");
scanf("%d",&c1);
printf("enter c2:");
scanf("%d",&c2);
if(c1!=r2)
{
printf("can't multiple these matrixes");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter the matrix [%d][%d]:",i,j);
scanf("%d",(x+i*c1+j));
}
}
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("enter the matrix [%d][%d]):",i,j);
scanf("%d",(y+i*c2+j));
}
}
printf("\nThe matrix multiplication of two matrix:");
printf("\n");
matmult(x,y,r1,r2,c1,c2);
return 0;
}

Test Cases: (Minimum 5 Test Cases)


1. enter r1:2
enter r2:2
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
enter c1:2
enter c2:2
enter the matrix [0][0]:2
enter the matrix [0][1]:3
enter the matrix [1][0]:4
enter the matrix [1][1]:5
enter the matrix [0][0]):5
enter the matrix [0][1]):4
enter the matrix [1][0]):3
enter the matrix [1][1]):2

The matrix multiplication of two matrix:


19 14
35 26

2. enter r1:2
enter r2:2
enter c1:2
enter c2:2
enter the matrix [0][0]:1
enter the matrix [0][1]:2
enter the matrix [1][0]:3
enter the matrix [1][1]:2
enter the matrix [0][0]):3
enter the matrix [0][1]):4
enter the matrix [1][0]):5
enter the matrix [1][1]):6

The matrix multiplication of two matrix:


13 16
19 24
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
3. enter r1:2
enter r2:2
enter c1:2
enter c2:2
enter the matrix [0][0]:3
enter the matrix [0][1]:4
enter the matrix [1][0]:5
enter the matrix [1][1]:6
enter the matrix [0][0]):7
enter the matrix [0][1]):8
enter the matrix [1][0]):9
enter the matrix [1][1]):0

The matrix multiplication of two matrix:


57 24
89 40

4. enter r1:2
enter r2:2
enter c1:2
enter c2:2
enter the matrix [0][0]:45
enter the matrix [0][1]:5
enter the matrix [1][0]:6
enter the matrix [1][1]:7
enter the matrix [0][0]):80
enter the matrix [0][1]):56
enter the matrix [1][0]):43
enter the matrix [1][1]):4

The matrix multiplication of two matrix:


3815 2540
781 364
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

5. enter r1:2
enter r2:2
enter c1:2
enter c2:2
enter the matrix [0][0]:23
enter the matrix [0][1]:23
enter the matrix [1][0]:23
enter the matrix [1][1]:23
enter the matrix [0][0]):23
enter the matrix [0][1]):23
enter the matrix [1][0]):
23
enter the matrix [1][1]):23

The matrix multiplication of two matrix:


1058 1058
1058 1058

Output: (Terminal Screen Shot)


1.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

2.

3.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

4.

4.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
5.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question 2: Write a C function that searches a given word in a line of text and
returns the frequency count. Make use of pointer notation

Code:
#include<stdio.h>
#include<string.h>
int func(char *a,int s,char word[],int *counter)
{
int i,j=0,k=0;
int wordc=1;
*counter=0;
char words[100][100];
for(i=0;i<s;i++)
{
if(*a!=' '&& *a!='.')
{
words[j][k]=*a;
k++;
a++;
}
else if(*a=='.')
{
j++;
k=0;
wordc++;
a++;
}
else
{
j++;
k=0;
wordc++;
a++;
}
}
for(i=0;i<wordc;i++)
{
if(strcmp(words[i],word)==0)
{
*counter=*counter+1;
}
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

}
int main()
{
int counter;
char line[100];
printf("Enter a line:");
gets(line);
int size=strlen(line);
char word[20];
printf("Enter a word to find:");
gets(word);
func(line,size,word,&counter);
printf("Counter=%d",counter);
return 0;
}

Test Cases: (Minimum 5 Test Cases)


1. Enter a line:you are the cause of my euphoria
Enter a word to find:euphoria
Counter=1

2. PS C:\Users\SSN\sri> ./a.exe
Enter a line:love yourself and speak yourself
Enter a word to find:yourself
Counter=2

3. Enter a line:nice apple and its poison apple but i am okay because i am jk...
Enter a word to find:apple
Counter=2
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
4. Enter a line:i had 4 apples and i ate two apples now how many apples i have?
Enter a word to find:apples
Counter=3

5. Enter a line:hello guys ! hello girls! hello ladeies! hello gentlemen!


Enter a word to find:hello
Counter=4

Output: (Terminal Screen Shot)


1.

2.

3.

4.

5.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question 3: Given multiple lines of text, parse the text to separate the tokens. A
token is a word separated by a space. Store the multiple lines of text as
individual strings whose maximum length is unspecified. Maintain a pointer to
each string within a one-dimensional array of pointers. Identify the last line of
text in some pre-determined manner. (Eg. END).

Code:
#include<stdio.h>
#include<string.h>

int end(char *c)


{
char m[4];
strcpy(m,"end");
int v = strcmp(c, m);

if (v == 0)
return 0;
else
return 1;
}

int main() {
char a[1000];
int b;
printf("Enter the sentence:");
gets(a);
b = end(&a);
while (b == 1)
{
gets(a);
b = end(&a);
}
return 0;
} a
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab

Test Cases: (Minimum 5 Test Cases)


1. i'm happy
my dog barks loudly
my college start at 8:00 pm#.
my college start at 8:00 pm.

2. we alwyas eat dinner together


they take the bus to work#.
they take the bus to work.

3. i don't wash the dishes


we see them ever week
time is very important in our life#.
time is very important in our life.

4. we all faces challenges


life teaches us many things#.
life teaches us many things.

5. i love jesus
i am biggest fan of heath ledger#.
i am biggest fan of heath ledger.

Output: (Terminal Screen Shot)


1.
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
2.

3.

4.

5.

Learning Outcomes:
To be proficient in using Pointers in C
a) Pointer notation for arrays and strings
b) passing parameters to a function by call-by-reference using pointers
c) constant pointers and pointers to constant data
d) dynamic memory allocation e) pointers to functions

You might also like