 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### C program for addition of two matrices in C

/* C program for addition of two matrices in C */ #include<stdio.h> int main() { int y, x, mat1, mat2, mat3; int row1, col1, row2, col2; printf("\nEnter the number of Rows of Mat1 : "); scanf("%d", &row1); printf("\nEnter the number of Cols of Mat1 : "); scanf("%d", &col1); printf("\nEnter the number of Rows of Mat2 : "); scanf("%d", &row2); printf("\nEnter the number of Columns of Mat2 : "); scanf("%d", &col2); /* Before accepting the Elements Check if no of rows and columns of both matrices is equal */ if (row1 != row2 || col1 != col2) { printf("\nOrder of two matrices is not same "); exit(0); } //Accept the Elements in Matrix 1 for (y = 0; y < row1; y++) { for (x = 0; x < col1; x++) { printf("Enter the Element a[%d][%d] : ", y, x); scanf("%d", &mat1[y][x]); } } //Accept the Elements in Matrix 2 for (y = 0; y < row2; y++) for (x = 0; x < col2; x++) { printf("Enter the Element b[%d][%d] : ", y, x); scanf("%d", &mat2[y][x]); } //Addition of two matrices for (y = 0; y < row1; y++) for (x = 0; x < col1; x++) { mat3[y][x] = mat1[y][x] + mat2[y][x]; } //Print out the Resultant Matrix printf("\nThe Addition of two Matrices is : \n"); for (y = 0; y < row1; y++) { for (x = 0; x < col1; x++) { printf("%d\t", mat3[y][x]); } printf("\n"); } return (0); }

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

C Program to find the biggest number in an array of numbers using recursion. Program prints the largest number in an unsorted array of elements using recursion. Array is...

C Program calculates the sum & average of an array. It declares an array and then add the array elements and finds the average of the array. Find the sum of negative numbers and

Program code to 'convert' uppercase string to lowercase using for loop. First check whether the given character is 'uppercase' alphabet or not. If it is Uppercase Alphabet just add 32 to

Structure declaration, condition for do-while loop, to input string, enter the infix string, to covert infix to prefix, checking for operand, to convert infix to postfix, to check for operand

C program 'Copy one String to another' string using loop. Input string from user and store it to some variable say "text1". Declare another variable to Store copy of first string in "text2".