Skip to content

Instantly share code, notes, and snippets.

@brijeshb42
Created May 13, 2014 14:46
Show Gist options
  • Save brijeshb42/af1cec4c5d43f02e49f2 to your computer and use it in GitHub Desktop.
Save brijeshb42/af1cec4c5d43f02e49f2 to your computer and use it in GitHub Desktop.
fscanf
/*
* make sure the input file is in same folder as the program
* output file will be generated in the same folder
*/
#include <stdio.h>
void readFile(char *fil){
FILE *fin, *fout; // fin for input, fout for output files
fin = fopen(fil,"r"); // open input file in read mode
fout = fopen("output.txt","w"); // open output file in write mode
int arr[101] = {0}; //initialise an array to store count of each number from 1 - 100
int tmp;
while(EOF != fscanf(fin,"%d",&tmp)){ // read number from file until the end of file
if(tmp>0 && tmp<=100){
arr[tmp]++; // increase the count
}
}
fclose(fin); // close input file
int i;
for(i=1;i<101;i++){
fprintf(fout, "%4d - %d\r\n", i,arr[i]); // write the count of each number to output file
}
fclose(fout); //close output file
printf("Output saved in output.txt\n");
}
int main(int argc, char const *argv[])
{
readFile("input.txt"); // pass the input filename
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int Matrix A[9][9] , MatrixB[9][9] , Matrixsproduct [9][9] ;
int n , i , j , k; /* 'i' used for rows and 'j' used for columns */
int Row1 , Row2 , Column1 , Column2;
clrscr();
printf(" Enter the order of Matrix A\n");
scanf("%d * %d " , &Row1 , &Column1);
printf(" Enter the order of Matrix B\n");
scanf("%d * %d " , &Row2 , &Column2);
if(Column1 == Row2)
{
printf(" Enter the elements of Matrix A\n");
for(i=0 ; i<Row1 ; i++)
{
for(j=0 ; j<Column1 ; j++)
{
scanf("%d" , &Matrix A[i][j] );
}
}
printf(" Enter the elements of Matrix B\n");
for(i=0 ; i<Row2 ; i++)
{
for(j=0 ; j<Column2 ; j++)
{
scanf("%d" , &Matrix B[i][j] );
}
}
/* Product of two Matrices */
for(i=0 ; i<Row1 ; i++)
{
for(j=0 ; j<Column2 ; j++)
{
Matrixproduct[i][j] = 0 ;
for(k=0 ; k<Row2 ; k++)
{
Matrixproduct[i][j] = Matrixproduct[i][j] + ( Matrix A[i][k] * Matrix B[k][j] );
}
}
}
printf(" Product Matrix\n");
for(i=0 ; i< Row1 ; i++)
{
for(j=0 ;j< Column2;j++)
{
printf("%d" , Matrixproduct[i][j] );
}
printf("\n");
}
} /* End of if */
else
{
printf(" Invalid order so Multiplication not possible\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment