Bubble sort Program by amclogon.in
#include
#include
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("enter the number of ele");
scanf("%d",&n);
printf("enter the number of ele");
for (i=0;ia[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array is \n");
for (i=0;i
Menu
Conditional and Logical Operators
Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if-else condition phrases. Conditional operator’s shape is:
Condition_phrase ? phrase1 : phrase2;
If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, thenphrase2 is executed, and whole phrase is assigned with phrase2 value.
Example:
int a, b, c;
...
c = a > b ? a : b; // if a>b "execute" a, else b
PRE-PROCESSOR’S COMMANDS
Before source code is translated (compiled), to a language “machine” understands, pre-processor is started. Depending on pre-processor’s commands, pre-processor changes the source code, and this changed code is now ready to be translated. Changes pre-processor made are saved into temp file, and the source code file is left untouched. Some of pre-processors commands are:
#include includes other files and links it with program
#define defines symbolic constant or so called macro
#include <stdio.h>
#include "MyFunctions.h"
#define PI 3.14159
#define PROGRAM NAME "Program"
With little help of pre-processor’s "define" command, macros can be produced (they remind of function but they aren’t one).
Example:
What’s the value assigned to b variable after following commands?
int a, b;
#define MAX(x,y) ((x) > (y)) ? (x) : (y)
a = 3;
b = MAX (3, a++);
/* Pre-processor physically changes this line with following:
b = ((3) > (a++)) ? (3) : (a++);
In the end values are
a = 5, b = 4 */
a = 3;
b = MAX (a++, 3);
/* Pre-processor physically changes this line with following:
b = ((a++) > (3)) ? (a++) : (3);
In the end values are
a = 4, b = 3 (3++ > 3) ? 3++ : 3*/
LOGIC OPERATORS
Simple relation phrases can be combined into more complex ones with help of logic operators. C language includes 3 logic operators.
Pay attention to difference between logic operators and logic operators above bits!
Example:
if ((x>20) && (x<100)) printf("x is inside open interval 20-100");
if ((x<5) || (x>20)) printf("x is not inside closed interval 5-20");
if (!(x>20)) printf("x is smaller or equal to 20");
Example:
What does following program block do?
int a, b, c, d;
a = 0;
b = 4;
c = (a++) + b; /*(a++) +b 0+4 4*/
printf ("a = %d, b = %d,c = %d " , a, b, c); /* 1, 4, 4 */
d = c && b + 3 * a;
printf ("d = %d", d);
Because priority of arithmetic operators is bigger then priority of logical operators:
d = c && b + 3 * a /* this is the same as expression in next row */
d = c && (b + (3 * a))
d = 4 && (4+ (3 * 1))
d = 4 && 7
d = 1
Complex phrases are often wrongly stated because people tend to “literally rewrite” stated conditions:
Statement “if x is higher than 20 and lower than 100” (pay attention that “lower than 100” reefers to x smaller than 100), is often wrongly (literally) written:
If (x>20 && <100) instead of correct one: If (x>20 && x<100)
This produces error translation. Another thing, people often mistake putting “,” instead of operator “AND”, so previous statement is again wrongly interpreted:
if(x>20 , x<100)
which produces “logic” error (which is much harder to notice, since compiler - translator program, can’t find it and report as error) .
Phrase "x>20,x<100" only matches the statement "x<100".
OPERATORS PRIORITY AND ASSIGNMENT
Term “assignment” reefers to assigning two operators of the same priority, one after another (from left to right and sideways). Table of priorities and assignment of all previously mentioned operators:
Pay attention that all comparing operators don’t use same priority!
Example:
Write your own program which calculates results (roots) of square equation(image placeholder).
Additional Info:
First #include <stdio.h> command "includes" header file with functions and constants description which reefer to work with input/output (inputting from keyboard/printing on screen).
Second #include <math.h> command "includes" header file with description of mathematical functions and constants.
When translating the code (linking), MS Visual C++ 6.0 includes data files with implementation of mathematical functions, by definition. When translating it isn’t necessary to give any additional parameters. When working with some Unix OS programming tools, C translator doesn’t include that data file by definition, so you have to add "-lm" parameter (option) manually (when starting translation).
#include <stdio.h>
#include <math.h>
main() {
float a, b, c, x1, x2, q, d, x1r, x2r, x1i, x2i;
printf("Input coefficients of square equation a,b,c:");
scanf("%f %f %f", &a, &b, &c);
d=b*b -4.0*a*c;
if (d > 0) {
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
} else if (d == 0) {
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
} else {
/* results are conjugated complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}
#include ALL HEADER FILES
math.h
#include <math.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program:
int abs (int x); IxIlong labs (long x); double fabs (double x); double sin (double x); double cos (double x); double tan (double x); double asin (double x); double acos (double x); double atan (double x); double sinh (double x); double cosh (double x); double tanh (double x); double exp (double x); exdouble log (double x); ln xdouble log10 (double x); log x double pow (double x,double y); xydouble sqrt(double x); sqare root of xdouble fmod(double x, double y); x mod y double ceil (double x); double floor(double x);
stdlib.h
#include <stdlib.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: void exit (int status); void randomize (void); or void srand (unsigned int seed); int rand (void);
string.h
#include <string.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t maxlen); char *strcat(char *dest, const char *src); size_t strlen(const char *s); char *strlwr(char *s); char *strupr(char *s); int strcmp(const char *s1, const char *s2); int strcmpi(const char *s1, const char *s2); int stricmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t maxlen); int strncmpi(const char *s1, const char *s2, size_t maxlen); int strnicmp(const char *s1, const char *s2, size_t maxlen); char *strchr(const char *s, int c); char *strstr(const char *string, const char *substring);
ctype.h
#include <ctype.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program:
int toupper(int ch); int tolower(int ch); int isdigit(int c); figure (0-9) int isalpha(int c); letter (A-Z or a-z) int isalnum(int c); letter (A-Z or a-z) or figure (0-9) int isprint(int c); character which can be printed (0x20-0x7E) int iscntrl(int c); control char (0x7F or 0x00-0x1F) int isspace(int c); empty space int islower(int c); letter (a-z) int isupper(int c); letter (A-Z)
alloc.h
#include <alloc.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: void *malloc (size_t size); NULL error void free (void *block); void *realloc(void *block, size_t size); NULL error
Write a C Program to Print the Multiplication of Two Matrices

Statement of C Program: This Program accepts two Matrices of different or same order and Find the product of these Matrices and prints the Product Matrix:
Condition: The Column of First Matrix must be Equal to the Row of the Second Matrix.

#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");
} /* End of main() */
Output:
Enter the order of Matrix A
2 * 2
Enter the order of MatrixB
2 * 2
Enter the elements of Matrix A
1
2
3
4
Enter the elements of Matrix B
5
6
7
8
Product Matrix
19 22
43 50
Subscribe to:
Posts (Atom)