Posts

Showing posts with the label C

Adding two Matrix (3 x 3) In C programming

The files "mata.txt" and "matb.txt" contain integer matrices of size 3 x 3. Write a program that calculates the sum of the matrices in a new matrix. The resulting sum matrix shall be saved to the file "sum.txt". #include<stdio.h> int main() {     FILE *filea, *fileb,*filec; //initialize the files     int arraya[3][3],arrayb[3][3],sum[3][3]; //initialize arrays which consists 3*3 elements       int i ,j; //PART A //We try to read from file mata.txt (filea).     if ((filea=fopen("mata.txt","r"))==NULL) // try to read from txt file      {         printf("Failed to open mata.txt");      }     else     {         for(i=0;i<3;i++)         {             for(j=0;j<3;j++){                 fscanf(filea,"%d",&arraya[i][j]); //reading f...

2D Array Explained Using Matrix

Image
2D Array Explained Let's create an array that has 2 dimensions like a matrix. Now let's give it some elements. Let's say int matrix[5][5] =  { 4, 6, 25, 88, 5, 34, 5, 300, 4, 65, 78, 43, 11, 90, 125, 98, 585, 12, 63, 21, 45, 35, 9, 5, 1 }; Now, we will see how to access the elements in the array. To do that we need to use nested for loop. Let's initialize two values i and j.  So, int i,j;    // Here i represents row and j represents column. That means when i=0 and j=0, it gets the first element from the matrix i.e 4 and when i=0 and j=1 it gets second element 6 and so on...  Now let's write whole C code to access the elements inside the array matrix. #include<stdio.h> int main() {     int i,j,sum;     int array[5][5]={             4, 6, 25, 88, 5,             34, 5, 300, 4, 65,          ...

Use of Array

Use of a 1D array Write a program that calculates the number of hours worked within a specific period and prints the total number of hours, the average length of a day and an itemization of the hours entered. First, the program must ask how many days of working hours shall be entered (max 30 days). After this, the program asks for the daily working hours. The program output shall have one decimal place of precision. #include <stdio.h> int main() {     printf("The program calculates the total hours worked during\na specific period and the average length of a day.\n");     int days,i;     float array[30],sum; //initilize array which can store 30 values or 120 bytes (4*30)     printf("How many days:");     scanf("%d",&days);     for (i=0; i<days; ++i)     {        printf("Enter the working hours for day %d:",i+1);        scanf("%f",& array[i]...

Finding Smallest Number Among Three (Using C and function)

I got this problem while learning C in school and its worth learning. And you can use same method for C++ also. Write a program that takes three integers as input and prints the smallest and largest of these numbers. The main program must prompt for integers and read them. Write the functions largest() and smallest() that receive the entered numbers as their parameters. Correspondingly, the functions shall return values corresponding to their names. #include <stdio.h> int largest(int first, int second, int third); //making the function name and giving parameters int smallest(int first, int second, int third); int main() {     int a,b,c,largestNumber,smallestNumber;     printf("Enter the 1. number: ");     scanf("%d",& a);     printf("Enter the 2. number: ");     scanf("%d",& b);     printf("Enter the 3. number: ");     scanf("%d",& c);     largestNumber=largest(a,b,c); ...