Pointers
Pointers
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
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;
}
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
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
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
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;
}
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
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>
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
5. i love jesus
i am biggest fan of heath ledger#.
i am biggest fan of heath ledger.
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