Sei sulla pagina 1di 23

TWO DIMENSIONAL DYNAMIC ARRAY

Creating a dynamic 2D array using Dynamic Memory Allocation and


accessing the elements using pointers.(without using [ and ]) and use the
dynamic 2D array to find the transpose of a matrix.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int row,col,i,j;
int **a;
clrscr();
printf("Enter row and column:");
scanf("%d%d",&row,&col);
a=(int **)calloc(row,sizeof(int));
for(i=0;i<col;i++)
{
*(a+i)=(int *)calloc(col,sizeof(int));
}
printf("Populate the array,-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Enter a[%d][%d]=",i,j);
scanf("%d",(*(a+i)+j));
}
}
printf("The inputted matrix is,-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",*(*(a+i)+j));
}
printf("\n");
}
printf("The transpose matrix is,-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",*(*(a+j)+i));
}
printf("\n");
}

getch();
return 0;
}

Write a C program to multiply two matrices using two dimensional dynamic


arrays.You must check the correctness of multiplication rules while inputting
the size of the matrices to be multiplied and also you must not use any of
[and ] symbols in your whole program code.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int r1,r2,c1,c2,i,j,k;
int **a1;
int **a2;
int **am;
clrscr();
printf("Enter the size of 1st matrix");
scanf("%d%d",&r1,&c1);
printf("Enter the size of 2nd matrix");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
printf("Wrong input!!!");
}
else
{
a1=(int **)calloc(r1,sizeof(int));
for(i=0;i<c1;i++)
{
*(a1+i)=(int *)calloc(c1,sizeof(int));
}
printf("Populate the 1st matrix,-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter a1[%d][%d]=",i,j);
scanf("%d",(*(a1+i)+j));
}
}
a2=(int **)calloc(r2,sizeof(int));
for(i=0;i<c2;i++)
{
*(a2+i)=(int *)calloc(c2,sizeof(int));
}

printf("Populate the 2nd matrix,-\n");


for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Enter a2[%d][%d]=",i,j);
scanf("%d",(*(a2+i)+j));
}
}
am=(int **)calloc(r1,sizeof(int));
for(i=0;i<c1;i++)
{
*(am+i)=(int *)calloc(c2,sizeof(int));
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
*(*(am+i)+j)=0;
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
*(*(am+i)+j)=(*(*(am+i)+j))+((*(*(a1+i)+k))*(*(*(a2+k)+j)));
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",*(*(am+i)+j));
}
printf("\n");
}
}
getch();
return 0;
}
THE STRUCTURES IN C LNGUAGE

Creating a structure and accessing the structure elements using . Dot


operator.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
struct student s;
system("clear");
printf("Enter Roll:");
scanf("%d",&(s.roll));
printf("Enter Name:");
scanf("%s",&(s.name));
printf("Enter Marks:");
scanf("%f",&(s.marks));
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",s.roll,s.name,s.marks);
return 0;
}

Creating a structure and accessing the structure elements using Arrow


operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
struct student s;
struct student *ptr;
ptr=&s;
system("clear");
printf("Enter Roll:");
scanf("%d",&(ptr->roll));
printf("Enter Name:");
scanf("%s",&(ptr->name));
printf("Enter Marks:");
scanf("%f",&(ptr->marks));

printf("Roll=%d\nName=%s\nMarks=%2.2f\n",ptr->roll,ptr->name,ptr->marks);
return 0;
}

Creating a structure and accessing the structure elements using


pointer(without using arrow operator).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
struct student s;
struct student *ptr;
ptr=&s;
system("clear");
printf("Enter Roll:");
scanf("%d",&((*ptr).roll));
printf("Enter Name:");
scanf("%s",&((*ptr).name));
printf("Enter Marks:");
scanf("%f",&((*ptr).marks));
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*ptr).roll,(*ptr).name,(*ptr).marks);
return 0;
}

Creating a dynamic structure using dynamic memory allocation and accessing


the structure elements using arrow operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{

struct student *ptr;


ptr=(struct student *)malloc(sizeof(struct student));
system("clear");
printf("Enter Roll:");
scanf("%d",&(ptr->roll));
printf("Enter Name:");
scanf("%s",&(ptr->name));
printf("Enter Marks:");
scanf("%f",&(ptr->marks));
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",ptr->roll,ptr->name,ptr->marks);
return 0;
}

Creating a dynamic structure using dynamic memory allocation and accessing


the structure elements using pointer(without using arrow operator).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
struct student *ptr;
ptr=(struct student *)malloc(sizeof(struct student));
system("clear");
printf("Enter Roll:");
scanf("%d",&((*ptr).roll));
printf("Enter Name:");
scanf("%s",&((*ptr).name));
printf("Enter Marks:");
scanf("%f",&((*ptr).marks));
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*ptr).roll,(*ptr).name,(*ptr).marks);
return 0;
}

Creating the array of structures and accessing the structure array elements
using . Dot operator.
#include<stdio.h>
#include<stdlib.h>

#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&(s[i].roll));
printf("Enter Name %d:",i+1);
scanf("%s",&(s[i].name));
printf("Enter Marks %d:",i+1);
scanf("%f",&(s[i].marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",s[i].roll,s[i].name,s[i].marks);
}
return 0;
}

Creating the array of structures and accessing the structure array elements
using Arrow operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");

printf("Enter the number of students:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((s+i)->roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((s+i)->name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((s+i)->marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(s+i)->roll,(s+i)->name,(s+i)>marks);
}
return 0;
}

Creating the array of structures and accessing the structure array elements
using pointer (without using Arrow operator).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));

}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*(s+i)).roll,(*(s+i)).name,
(*(s+i)).marks);
}
return 0;
}

Creating a dynamic array of structures using dynamic memory allocation and


accessing the structure array elements using . Dot operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student *s;
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
s=(struct student *)malloc(n*sizeof(struct student));
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&(s[i].roll));
printf("Enter Name %d:",i+1);
scanf("%s",&(s[i].name));
printf("Enter Marks %d:",i+1);
scanf("%f",&(s[i].marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",s[i].roll,s[i].name,s[i].marks);
}
return 0;
}

Creating a dynamic array of structures using dynamic memory allocation and


accessing the structure array elements using Arrow operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student *s;
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
s=(struct student *)malloc(n*sizeof(struct student));
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((s+i)->roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((s+i)->name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((s+i)->marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(s+i)->roll,(s+i)->name,(s+i)>marks);
}
return 0;
}

Creating a dynamic array of structures and accessing the structure array


elements using pointer (without using Arrow operator).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
int roll;
char name[50];

float marks;
} goru;
int main(void)
{
int i,n;
goru *s;
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
s=(goru *)malloc(n*sizeof(goru));
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*(s+i)).roll,(*(s+i)).name,
(*(s+i)).marks);
}
return 0;
}
FILE PROGRAMMING IN C

Program to open a text file test.text if it exists, otherwise showing the


message if it doesnt exist or failed to open.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
FILE *fp;
fp=fopen("E:\\test.txt","r");
if(fp==NULL)
{
fputs("Cannot Open File!!!",stderr);
exit(1);
}
else
{

printf("File Opened Successfully\n");


fclose(fp);
}
return 0;
}

Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char ch;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
for(count=0;((ch=getc(fp))!=EOF)&&(!feof(fp));count++)
{
printf("%c",ch);
}
printf("\n%d characters read",count);
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);

}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}

Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char *str;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
str=(char *)calloc(MAX_SIZE,sizeof(char));
for(count=0;!feof(fp);count++)
{
fscanf(fp,"%s",str);
printf("%s%c",str,getc(fp));
}
fclose(fp);

}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}

Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char *str;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
str=(char *)calloc(MAX_SIZE,sizeof(char));
for(count=0;fread(str,sizeof(str),1,fp)==1&&!feof(fp);count++)

{
printf("%s",str);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}

Program to write the contents of a dynamic array of structure into a file. If


the file already exist append the structure array contents, create a new file
and save the contents otherwise.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
typedef struct student
{
int roll;
char name[50];
float marks;
} stud;
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};

int main(void)
{
char file[50]="e://student.txt";
FILE *fp;
int count,i,n;
stud *s;
printf("Enter the no of students:");
scanf("%d",&n);
if(isFileExist(file))
{
if(fp=fopen(file,"a+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fprintf(fp,"Record %d\n%d\n%s\n%f\n",i+1,(*(s+i)).roll,(*(s+i)).name,(*(s+i)).marks);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
if(fp=fopen(file,"w+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*s).roll));
printf("Enter name %d:",i+1);

scanf("%s",&((*s).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*s).marks));
}
for(i=0;i<n;i++)
{
fprintf(fp,"Record %d\n%d\n%s\n%f\n",i+1,(*s).roll,(*s).name,(*s).marks);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
return 0;
}

Program to write the contents of a dynamic array of structure into a file. If


the file already exist append the structure array contents, create a new file
and save the contents otherwise.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
typedef struct student
{
int roll;
char name[50];
float marks;
} stud;
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}

};
int main(void)
{
char file[50]="e://student.txt";
FILE *fp;
int count,i,n;
stud *s;
char str[50];
printf("Enter the no of students:");
scanf("%d",&n);
if(isFileExist(file))
{
if(fp=fopen(file,"a+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fwrite(s+i,sizeof(stud),1,fp);
}
printf("File saved successfully\n");
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
if(fp=fopen(file,"w+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*s).roll));

printf("Enter name %d:",i+1);


scanf("%s",&((*s).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*s).marks));
}
for(i=0;i<n;i++)
{
fwrite(s+i,sizeof(stud),1,fp);
}
printf("File saved successfully\n");
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
return 0;
}

Now some example programs


Creating Dynamic Array of Structures and Accessing the structure elements,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct student
{
int roll;
char name[30];
float marks;
} pranay;
int main(void)
{
int n,i;
clrscr();
pranay *s;
printf("Enter the no of students:");
scanf("%d",&n);
s=(pranay *)calloc(n,sizeof(pranay));
if(s!=NULL)
{
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));

printf("Enter name %d:",i+1);


scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
printf("Student Database:\n-----------------\n");
for(i=0;i<n;i++)
{
printf("Student Record#%d\n",i+1);
printf("Roll:%d\n",(*(s+i)).roll);
printf("Name:%s\n",(*(s+i)).name);
printf("Marks:%3.2f\n",(*(s+i)).marks);
}
}
getch();
return 0;
}

Now save the structure elements into a file name called studentRecord.txt, if
the file exists and contains data previously show the previously saved data and
append new data into it.
#include<stdio.h>
#include<conio.h>
typedef struct student
{
int roll;
char name[30];
float marks;
} pranay;
int main(void)
{
int n,i,count=0;
clrscr();
pranay *s,*r;
FILE *fp;
if((fp=fopen("E:\\studentRecord.txt","a+"))==NULL)
{
fputs("File Error!!!",stderr);
exit(1);
}
else
{
r=(pranay *)calloc(10,sizeof(pranay));
rewind(fp);
for(i=0;fread(r+i,sizeof(pranay),1,fp)==1;i++)

{
printf("Student Record#%d\n",i+1);
printf("Roll:%d\n",(*(r+i)).roll);
printf("Name:%s\n",(*(r+i)).name);
printf("Marks:%3.2f\n",(*(r+i)).marks);
}
//Another way to perform this
/*while(fread(&r[count],sizeof(pranay),1,fp)==1)
{
count++;
}*/
count=i;
printf("File records=%d\n",count);
printf("Enter the no of students:");
scanf("%d",&n);
s=(pranay *)calloc(n,sizeof(pranay));
if(s!=NULL)
{
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fwrite(s+i,sizeof(pranay),1,fp);
}
}
fclose(fp);
}
getch();
return 0;
}

Now rewriting the above program using the same dynamic array of structures
to read from the file and also to append new data into the file and also
changing the type of the file (studentRecord.txt) from text file to binary file.
#include<stdio.h>
#include<conio.h>
#define MAX_STUDENT 50
typedef struct student
{

int roll;
char name[50];
float marks;
} pranay;
int main(void)
{
int n,i,count=0;
clrscr();
FILE *fp;
pranay *s;
s=(pranay *)calloc(MAX_STUDENT,sizeof(pranay));
if((s==NULL)||((fp=fopen("E:\\studentRecord.bin","a+b"))==NULL))
{
fputs("Error!!!",stderr);
exit(1);
}
else
{
rewind(fp);
for(i=0;fread(s+i,sizeof(pranay),1,fp)==1;i++)
{
printf("Student Record#%d\n",i+1);
printf("Roll:%d\n",(*(s+i)).roll);
printf("Name:%s\n",(*(s+i)).name);
printf("Marks:%3.2f\n",(*(s+i)).marks);
}
count=i;
printf("\n%d existing records retrieved\n",count);
printf("Enter the no of students:");
scanf("%d",&n);
if(n<=(MAX_STUDENT-count))
{
for(i=count;i<count+n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
fwrite(s+count,sizeof(pranay),n,fp);
}
else
{
printf("Exeeds intake\n");

}
}
fclose(fp);
getch();
return 0;
}

Write a C program to read BookTitle <string>, Author <string>, and


BookId ,<integer> for several books and store it into a temporary structure
named BookRecord. Finally store the structure contents into a text file by
the name of BookTitle, that is <BookTitle>.txt. If the file already exists
notify with File Already exists message.

Potrebbero piacerti anche