## Welcome to the World of Modelling and Simulation

### What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

### Some Basic Examples of C Programming: Part 2

// Application of goto statement

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
int i, j;

for(i=0; i<10; i++)
{
printf("Outer loop executing. i = %d\n",i);
for(j=0; j<3; j++)
{
printf("Inner loop executing. j = %d\n",j);
if(i==5)
goto stop;
}
}
/* This message does not print: */
printf("Loop exited. i = %d\n",i);
stop: printf("Jumped to stop. i = %d\n",i);

getch ();
}

-----------------------------------------------------------------------------------------
// Example of goto statement

#include <stdio.h>
#include <conio.h>
main ()
{
int x;
printf("enter a number");
scanf("%d",&x);
odd:
printf("\nThe number is %d odd",x);
if(x%2==0)
{goto even;}
elseif
{goto odd;}
even:
printf("\nThe number is %d even",x);

getch ();
}

--------------------------------------------------------------------------------------------
//break statement

#include <stdio.h>
#include <conio.h>

main ()
{int i;

for(i=0; i<100; i++)
{
printf("\n%d",i);
if(i==10)
break;
}
getch ();
}

-----------------------------------------------------------------------------------------------
//break statement

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
int m;
float x,sum,average;
printf("This program computes the average of a set of numbers\n");
printf("Enter values one after another\n");
printf("Enter a negative number at the end\n\n");
sum=0;
for(m=1;m<=1000;++m)
{
scanf("%f",&x);
if(x<0)
break;
sum+=x;}
average=sum/(m-1);
printf("\n");
printf("Number of values = %d\n",m-1);
printf("Sum              = %f\n",sum);
printf("Average          = %f\n",average);
getch ();}

-------------------------------------------------------------------------------------------------
//continue statement

#include <stdio.h>
#include <conio.h>

main ()
{int i;

for(i=0; i<100; i++)
{
if(i==10)
continue;
printf("\n%d",i);
}

---------------------------------------------------------------------------------------------------
//Example of array: lowercase text to uppercase

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

#define SIZE 10

main ()
{
char letter[SIZE];
int count;

for(count=0; count<SIZE; ++count)
{letter[count]=getchar();}

for(count=0; count<SIZE; ++count)
{putchar(toupper(letter[count]));}

getch ();}

getch ();
}

-----------------------------------------------------------------------------------------------------
// One Dimensional Array

#include <stdio.h>
#include <conio.h>

main ()
{
int numbers[]={1,2,3,4,5};
int i;
printf("Array elements are\n");

for(i=0; i<=4; i++)
printf("%d\n",numbers[i]);
getch ();
}

------------------------------------------------------------------------------------------------------
//Example of one dimensional array: To find out the largest number

#include <stdio.h>
#include <conio.h>

main ()
{
int num[5] = {2, 90, 102, 13, -76};
int largest = num[0], i = 0;
for(i=0; i<5; i++){
if(num[i] > largest)
{
largest = num[i];
}
}
printf("Largest number is %d\n", largest);
getch ();
}

---------------------------------------------------------------------------------------------------------
// Program to illustrate one dimensional array (sum of the squares of ten real numbers)

#include <stdio.h>
#include <conio.h>

main ()
{
int i;
float x[10],value,total;
printf("Enter 10 Real Numbers\n");

for(i=0; i<10; i++)
{
scanf("%f",&value);
x[i]=value;
}
total=0;

for(i=0; i<10; i++)
{total=total+x[i]*x[i];}

printf("\n");
for(i=0; i<10; i++)
{printf("x[%d]=%f\n",i,x[i]);}
printf("\ntotal=%f\n",total);

getch ();}

-------------------------------------------------------------------------------------------------------------
// Calculation of the average of n numbers and then computation of the deviation of each number about the average

#include <stdio.h>
#include <conio.h>

main ()
{
int n,count;
float avg,d,sum=0;
float list[100];

printf("\nHow many numbers will be averaged?");
scanf("%d",&n);
printf("\n");

for(count=0; count<n; ++count)
{printf("i=%d x=",count);
scanf("%f",&list[count]);
sum+=list[count];
}
avg=sum/n;
printf("\nThe average is %f\n\n",avg);

for(count=0; count<n; ++count)
{
d=list[count]-avg;
printf("i=%d x=%f d=%f\n", count,list[count],d);
}
getch ();
}

----------------------------------------------------------------------------------------------------------------
// Two Dimensional Array

#include <stdio.h>
#include <conio.h>

main ()
{
int a[3][3], i, j;
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
}
}
printf("\n\t Matrix is : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\t %d",a[i][j]);  //print 3*3 array
}
printf("\n");
}
getch ();
}

---------------------------------------------------------------------------------------------------------------
// Program to print multiplication table using two dimensional array

#include <stdio.h>
#include <conio.h>
#define ROWS 5
#define COLUMNS 5

main ()
{
int row,column,product[ROWS][COLUMNS];
int i,j;
printf("\t\t MULTIPLICATION TABLE \n\n");
printf("\t");
for(j=1; j<=COLUMNS; j++)
{printf("%d\t",j);}
printf("\n");
printf("___________________________________________\n");
for(i=0; i<ROWS; i++)
{
row=i+1;
printf("%d|\t",row);
for(j=1; j<=COLUMNS; j++)
{
column=j;
product[i][j]=row*column;
printf("%d \t",product[i][j]);
}
printf("\n");
}
getch ();}

----------------------------------------------------------------------------------------------------------
// Three Dimensional Array

#include <stdio.h>
#include <conio.h>

main ()
{
int numbers[2][1][3];
int i,j,k;
printf("Enter Table Values\n");
for(i=0; i<2; i++)
{
printf("Table %d:\n",i+1);
for(j=0; j<1; j++)
{
for(k=0; k<3; k++)
{
scanf("%d",&numbers[i][j][k]);
}}}

for(i=0; i<2; i++)
{
printf("Table %d:\n",i+1);
for(j=0; j<1; j++)
{
for(k=0; k<3; k++)
{
printf("%d \t",numbers[i][j][k]);}
printf("\n");
}}
getch ();}

---------------------------------------------------------------------------------------------------------
// character array

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

main ()
{
char word1[40],word2[40],word3[40],word4[40];
printf("Enter text:\n");
scanf("%s%s",word1,word2);
scanf("%s",word3);
scanf("%s",word4);
printf("\n");
printf("word1=%s\nword2=%s\n",word1,word2);
printf("word3=%s\nword4=%s\n",word3,word4);
getch ();}

-----------------------------------------------------------------------------------------------------------
// User Defined Function: to determine the largest of three integer quantities

#include <stdio.h>
#include <conio.h>
#include <math.h>

int maximum(int x, int y)
{
int z;
z=(x>=y)?x:y;
return(z);
}

main ()
{
int a,b,c,d;
printf("\na=");
scanf("%d",&a);
printf("\nb=");
scanf("%d",&b);
printf("\nc=");
scanf("%d",&c);

d=maximum(a,b);
printf("\n\nmaximum=%d",maximum(c,d));

getch ();}

---------------------------------------------------------------------------------------------------------
User Defined Function: to calculate the factorial of an integer quantity

#include <stdio.h>
#include <conio.h>
#include <math.h>

long int factorial(int n);

main ()
{
int n;
printf("\nn=");
scanf("%d",&n);
printf("\nn!=%ld",factorial(n));

getch ();}

long int factorial(int n)
{
int i;
long int product=1;
if(n>1)
for(i=1; i<=n; ++i)
product*=i;
return(product);
}

-------------------------------------------------------------------------------------------------------------
User Defined Function

#include <stdio.h>
#include <conio.h>
#include <math.h>

void printline(void);

main ()
{
printline();
printf("This illustrates the use of C functions\n");
printline();

getch ();
}

void printline(void)
{
int i;
for(i=1; i<40; i++)
printf("_");
printf("\n");
}