0% found this document useful (0 votes)
5 views8 pages

C Programs for File Copy, Matrix, and Sorting

This C program contains code for multiple functions: 1) A file copy program that takes in source and destination file names from the user and copies the contents of the source file to the destination file. 2) A matrix multiplication program that takes in two 3x3 matrices from the user, multiplies them together, and displays the result. 3) A program that uses a structure to store student data like name, ID, and age and displays the stored values. 4) A program that takes in an array of numbers from the user, sorts it in ascending and descending order, and displays the results. 5) A program that takes in two numbers, calculates their greatest common divisor (GCD)

Uploaded by

Sujay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

C Programs for File Copy, Matrix, and Sorting

This C program contains code for multiple functions: 1) A file copy program that takes in source and destination file names from the user and copies the contents of the source file to the destination file. 2) A matrix multiplication program that takes in two 3x3 matrices from the user, multiplies them together, and displays the result. 3) A program that uses a structure to store student data like name, ID, and age and displays the stored values. 4) A program that takes in an array of numbers from the user, sorts it in ascending and descending order, and displays the results. 5) A program that takes in two numbers, calculates their greatest common divisor (GCD)

Uploaded by

Sujay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Program to copy content of a file to another

#include <stdio.h>

main()

FILE *fs,*fd;

char ch,src[13],des[13];

printf("***File copy program***");

printf("\nEnter the source file name:");

scanf("%s",src);

fs=fopen(src,"r");

if(fs==NULL)

printf("\nFIle doesn't exist or cannot open");

getch();

exit(0);

printf("\nEnter the destination file name:");

scanf("%s",des);

fd=fopen(des,"w");

if(fd==NULL)

printf("\nFile doesn't exist or cannot open");


fclose(fs);

getch();

exit(0);

while(1)

ch=fgetc(fs);

if(ch==EOF)

break;

fputc(ch,fd);

printf("\nFile copied successfully");

printf("\nFRom %s to %s ",src,des);

fclose(fs);

fclose(fd);

getch();

}
//Program for NxM matrix
#include<stdio.h>
#define SIZE 3
int main()
{
int A[SIZE][SIZE];
int B[SIZE][SIZE];
int C[SIZE][SIZE];
int row,col,i,sum;
printf("\tEnter the elements in matrix A of size %dx%d;\n",SIZE,SIZE);
for(row=0;row<SIZE;row++)
{
for(col=0;col<SIZE;col++)
{
scanf("%d",&A[row][col]);
}
}

printf("\t\n Enter elements in matrix B of size %dx%d;\n",SIZE,SIZE);


for(row=0;row<SIZE;row++)
{
for(col=0;col<SIZE;col++)
{
scanf("%d",&B[row][col]);
}
}

for(row=0;row<SIZE;row++)
{
for(col=0; col<SIZE;col++)
{
sum=0;
for(i=0;i<SIZE;i++)
{
sum+=A[row][i]*B[i][col];
}
C[row][col]=sum;
}
}
printf("\nProduct of Matrix A*B=\n");
for(row=0;row<SIZE;row++)
{
for(col=0;col<SIZE;col++)
{
printf("\t%d",C[row][col]);
}
printf("\t\n");
}
return 0;
}
//program to using structures-studentinfo
#include <stdio.h>
struct StudentData
{
char *stu_name[10];
int stu_id;
int stu_age;
};
int main()
{ /*student is the variable of structure*/
struct StudentData student;
/*Assigning the values of each struct members*/
printf("Enter the Student's name\n");
scanf("%s",&student.stu_name);
printf("Enter the Student's Id\n");
scanf("%d",&student.stu_id);
printf("Enter the Student's Age\n");
scanf("%d",&student.stu_age);
/*Displaying the values of each struct members*/
printf("Student Name is : %s\n",student.stu_name);
printf("\nStudent Id is : %d\n",student.stu_id);
printf("\nStudent age is : %d\n",student.stu_age);
return 0;
}
//program for ascending and descending
#include<stdio.h>
main()
{
int n,data[100],i,j,temp;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element %d:",i+1);
scanf("%d",&data[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(data[i]>data[j])
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
printf("\n Ascending order:");
for(i=0;i<n;i++)
printf("\t%d",data[i]);
printf("\n Descending order;");
for(i=n-1;i>=0;i--)
printf("\t %d",data[i]);
return 0;
}
//program for lcm and gcd
#include <stdio.h>

int main()

int num1,num2,gcd,lcm,remainder,numerator,denominator;

printf("Enter the numbers:\n");

scanf("%d%d",&num1,&num2);

if(num1>num2)

numerator=num1;

denominator=num2;

else

numerator=num2;

denominator=num1;

remainder=numerator%denominator;

while(remainder!=0)

numerator=denominator;
denominator=remainder;

remainder=numerator%denominator;

gcd=denominator;

lcm=num1*num2/gcd;

printf("GCD of %d and %d =%d\n",num1,num2,gcd);

printf("LCM of %d and %d =%d\n",num1,num2,lcm);

return 0;

getch();

Common questions

Powered by AI

The C program sorts an array of integers using a simple comparison-based sorting algorithm called Bubble Sort. For ascending order, it iterates through the array using two nested loops, comparing adjacent elements and swapping them if they are out of order. After sorting in ascending order, it prints the elements. For descending order, the program prints the elements of the array starting from the last element to the first, effectively reversing the order .

Using 'getch()' is a non-standard C library function, typically specific to certain compilers (e.g., Turbo C), which can cause portability issues when running the program on different systems or compilers. As it waits for a key press, its impact is mainly aesthetic and unnecessary for logical file handling tasks. Alternative methods for similar functionality that don't affect portability include standard functions like 'getchar()', allowing the program to compile and run across varying platforms without modification .

The program ensures safety by closing each file stream after its operations are completed using 'fclose(fs)' for the source file and 'fclose(fd)' for the destination file. This proper closure prevents memory leaks and data corruption by ensuring all buffered operations are completed and resources are deallocated. The program structure ensures these calls are made regardless of whether the file operations were successful, thereby maintaining integrity and preventing resource wastage .

The error that might occur is if either the source or the destination file cannot be opened. This condition is checked using 'if(fs==NULL)' for the source file and 'if(fd==NULL)' for the destination file, where 'fs' and 'fd' are file pointers. If either file cannot be opened, the program prints a message indicating that the file doesn't exist or cannot be opened, and then it exits using 'exit(0)' after closing any previously opened file stream .

The program multiplies two 3x3 matrices using three nested loops. The outer two loops iterate over each row and column of the resultant matrix 'C'. The innermost loop iterates over elements of row 'A' and column 'B', multiplying them and adding the result to 'sum', which represents the element at position 'C[row][col]'. After all multiplications and additions for a specific element are complete, 'sum' is assigned to 'C[row][col]'. This method ensures that all matrix multiplication rules are followed .

The Euclidean algorithm is used in the program to calculate GCD by repeatedly replacing the larger number by the remainder of dividing the larger by the smaller until the remainder is 0. The last non-zero remainder is the GCD. This method is efficient because it reduces the size of numbers involved with each step, thus converging quickly to the correct GCD. Once the GCD is calculated, the LCM is derived using the relation: LCM = (num1*num2)/GCD, ensuring that the calculation is both quick and memory efficient .

Checking which number is greater is crucial for setting 'numerator' and 'denominator' correctly, as the Euclidean algorithm requires division where the larger number is divided by the smaller. This initial step simplifies the iterative process of reducing the remainder to eventually find the GCD. Incorrectly identifying these could lead to errors or unnecessary iterations, making the process inefficient .

The program uses a structure named 'StudentData' with elements: 'stu_name' (array of character pointers), 'stu_id', and 'stu_age'. The potential error lies in using 'char *stu_name[10]' which is an array of pointers, likely intended for a single string. Using 'scanf' with '&student.stu_name' is incorrect and may lead to undefined behavior since it would expect a pointer to a buffer. A better practice is to use 'char stu_name[10]' for direct storage of a name string .

Key considerations include checking whether files can be opened, as indicated by 'fopen' returning NULL. Proper error handling involves closing any files that were successfully opened if subsequent operations fail, ensuring resources are not wasted. The program also demonstrates proper sequencing by opening the source file first, reading until EOF, and then writing to the destination, ensuring that file operations do not overlap improperly .

To improve flexibility, the program could be modified to dynamically allocate memory for matrices using pointers and allocate exact sizes based on runtime input. Instead of using constants, taking user input can define 'SIZE' dynamically. This approach would allow handling matrices of different dimensions and sizes beyond 3x3, making the program more versatile for varied applications .

You might also like