Module 7: Introduction TO C And C++

Google+ Pinterest LinkedIn Tumblr +

MODULE 7

————————————————-
#include
//for system()
#include


{
C++ codes…
}
————————————————-
should be changed to:
————————————————-
#include
//use C++ wrapper to call C functions from C++ programs…
#include
using namespace std;


{
C++ codes…
}
————————————————-
In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore.
It should be C++ header, so that it comply to the standard.
In older Borland C++ compiler this still works, but not proper any more…
and for standard C/C++ the portability should be no problem or better
you read Module23 at http://www.tenouk.com/Module23.html to get
the big picture…For C codes, they still C codes :o)
=========================================================================
=========================================================================

//Program to find the total of all the elements in array y
//Change the header files accordingly for C++ codes…
//#include
//#include
//using namespace std;

#include
#include
//replace every n occurrences with 7
#define n 7

int main()
{
int i, total = 0, y[n]= {6,9,2,4,5,23,12};

for (i=0; i {
//display the array contents…
cout< #include
#include
#define m 3
#define n 3

int main()
{
int i, j;
int x[m][n]={{10,25,33}, {21,32,43},{20,42,51}};

cout<<“\n3x3 arrays’ subscripts and\n”;
cout<<“their respective elements\n”;
cout<<“————————–\n”;

//outer for loop, reading the row by row…
for(i=0; i //inner loop, for every row, read every column by column…
for(j=0; j cout<<“[“<
system(“pause”);
return 0;
}

———————————————————————————-

//using two-dimensional array to compute the
//average of all the elements in array named x
#include
#include
#define m 4
#define n 5

int main()
{
int i, j, total = 0;
//4×5 or [4][5] array variable with initial values…
int q[m][n]={{4,5,6,2,12},{10,25,33,22,11},{21,32,43,54,65},{3,2,1,5,6}};

float average;

//outer for loop, read row by row…
for(i=0; i //inner for loop, for every row, read column by column
for(j=0; j //get the summation of the array elements.
{
//display the array…
cout<<“q[“< total=total + q[i][j];
}
//calculate the average
//simple typecast from int to float…
average = (float)total/(float) (m*n);

cout<<“\nThis program will calculate the average of the”;
cout<<“\n4 x 5 array, which means the sum of the”;
cout<<“\narray’s element, divide the number of the”;
cout<<“\narray’s element….”;
cout<<“\nProcessing…. PLEASE WAIT\n”;

//display the average
cout<<“Average = “< cout<<“\nThe Average = “<
system(“pause”);
return 0;
}

——————————————————————————–

//program to compute the square root of the sum
//of the squares of all the elements in array x
#include
#include
#include
#define m 4
#define n 5

int main()
{
int i, j;
int x[m][n]={{4,5,6,2,12},{10,25,33,22,11},{21,32,43,54,65},{3,2,1,5,6}};

float sum2, result;

//outer for loop, read row by row…
for(i=0; i {//inner for loop, for every row, read column by column
for(j=0; j {
//set some condition here to avoid divides by 0…
if(x[i][j]>0)
//do the square of the array elements and then sum up…
sum2 = sum2 + pow(x[i][j], 2);
}
//assign the result to variable result…
//do the square root of the previous result….
result = sqrt(sum2);
}

//some story and printing the result…
cout<<“\nFirst, summing up all the arrays’ element”;
cout<<“\nThe given array has 4 x 5 in size,\n”;
cout<<“\nThe sum is = “< cout<<“\nNext, square root the sum\n”;
cout<<“\nThe answer is = “<
system(“pause”);
return 0;
}

———————————————————————————-

//multiplication of the matrix x and matrix
//y and stores the result in matrix z
#include
#include
#define m 3
#define c 2
#define n 4

int main()
{
int i, j, k;
//first matrix…
int x[m][c]= {{1,2},{3,4},{5,6}};
//second matrix…
int y[c][n]= {{7,8,9,10},{11,12,13,14}};
//for storing the matrix product result…
int z[m][n];

for(i=0; i for(j=0; j {
z[i][j]= 0;
for(k=0; k //same as z[i][j]= z[i][j]+ x[i][k]* y[k][j];
z[i][j]+= x[i][k]* y[k][j];
}
cout<<“\nMultiply matrix x and matrix y,”;
cout<<“\nThen store the result in matrix z.”;
cout<<“\nMatrix x is 3×2, and matrix y is 2×4,”;
cout<<“\nso, the result, z should be matrix 3×4\n”;
cout<<“\nThe matrix product is: \n”;

for (i=0; i {
cout<<“\n”;
for(j=0; j //display the result…
cout<<” “< }
cout<
system(“pause”);
return 0;
}

——————————————————————————–

//Compute the sum of the elements of an array
#include
#include
#define SIZE 12

int main()
{
//declare and initialize the array named a
//with size SIZE
int a[SIZE] = {1,3,5,4,7,2,99,16,45,67,89,45};
//declare two normal variables
int i, total = 0;

//do the loop for the array…
for(i = 0; i <= (SIZE-1); i++)
{
//display the array and its element…
printf(“\n a[%d]= %d”, i, a[i]);
//total up the array
//total = total + a[i] total += a[i];
}

printf(“\nThe sum of the array elements is %d\n”, total);

system(“pause”);
return 0;

}

——————————————————————————-

//Printing simple histogram
#include
#include
#define SIZE 10

int main()
{
//declare and initialize an array named n
//with size SIZE…
int n[SIZE] = {19, 3, 15, 7, 11, 9, 13, 5, 17, 1};
int i, j;

//display the table header…
printf(“%sss\n”,”Element/index”, “Value”, “Histogram”);

//do the iteration…
//outer for loop, read row by row…
for(i=0; i <= (SIZE-1); i++)
{
printf(“�d “, i, n[i]);
//inner for loop, for every row, read column
//by column and print the bar…
for(j = 1; j<= n[i]; j++)
//print bar…repeat…
printf(“*”);
//go to new line for new row…repeats…
printf(“\n”);
}

system(“pause”);
return 0;
}

——————————————————————————-

//Sorting an array values into ascending order
#include
#include
#define SIZE 10

int main()
{
int a[SIZE] = {34,6,41,58,0,12,89,-2,45,25};
int i, pass, hold;

printf(“Data items in original order\n\n”);
//displaying the original array…
for(i=0; i<=SIZE – 1; i++)
printf(“%d “, a[i]);

//——do the sorting…ascending————-
//for every array elements do this…
for(pass = 1; pass <= (SIZE-1); pass++)
//for every 2 array elements comparison do
//the comparison and swap…
for(i = 0; i <= (SIZE-2); i++)
//set the condition…
if(a[i]> a[i + 1])
{
//put the a[i]in temporary variable hold…
hold = a[i];
//put the a[i + 1]in a[i] a[i]= a[i + 1];
//put the hold in a[i + 1], one swapping is
//completed…and repeat for other elements…
a[i + 1]= hold;
}

printf(“\n\nData items in ascending order\n\n”);
//display the new ordered list…
for (i=0; i <= (SIZE-1); i++)
printf(“M”, a[i]);
printf(“\n\n”);
system(“pause”);
return 0;
}

————————————————————————————–

//Initializing multidimensional arrays
//and function
#include
#include

//function prototype
void printArray(int [][3]);

int main()
{
//declare 3 array with initial values…
int array1[2][3] = {{1,2,3}, {4,5,6}},
array2[2][3] = {{1,2,3},{4,5}},
array3[2][3] = {{1,2}, {4}};

printf(“Element values in array1 by row are: \n”);
//first time function call
printArray(array1);
printf(“\nElement values in array2 by row are: \n”);

//second time function call
printArray(array2);
printf(“\nElement values in array3 by row are:\n”);

//third time function call
printArray(array3);
printf(“\nNOTICE THE DEFAULT VALUE 0…\n”);
system(“pause”);
return 0;
}

//function definition, passing an array to function
void printArray(int a[][3])
{
int i, j;
//outer for loop, read row by row…
for(i = 0; i <= 1; i++)
{
//inner for loop, for every row, read column by column…
for(j=0; j<= 2; j++)
{
printf(“[%d][%d] = %d “, i, j, a[i][j]);
}
printf(“\n”);
}
}

———————————————————————————–

//program will sort a list of
//a strings entered by the user
#include
#include
#include

int main()
{
//declare two arrays named tname with 1-Dimension
//and name with 2-Dimension
char tname[20], name[20][20];
//normal variables…
int i, j, n;

cout<<“Enter the number of names: “;
cin>>n;

//outer loop for counter…
for(i=0; i {
cout<<“\nEnter the name(one word) “<<(i+1)<<“: “;
cin>>name[i];
}

//inner for loop, read row by row set outer for loop…
for(i=0; i //innermost for loop, read column by column of the characters…
for(j = i+1; j //set the condition…
//strcmp – compare the string standard library function
//do the sorting…
if(strcmp(name[i], name[j])>0)
{
//strcpy – copy the strings…
//compare and swap…
strcpy(tname, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], tname);
}
cout<<“\nSorted names:\n”;
for (i =0; i cout<<“\n”< cout<
system(“pause”);
return 0;
}

————————————————-VC++/VC++ .Net——————————————————

//Sorting array values into ascending order
#include
#define SIZE 10

int main()
{
int a[SIZE] = {-4,6,3,-20,0,1,77,-2,42,-10};
int i, pass, hold;

printf(“Data items in original order\n\n”);

//displaying the original array…
for(i=0; i<=SIZE – 1; i++)
printf(“%d “, a[i]);

//——do the sorting…ascending————-
//for every array elements do this…
for(pass = 1; pass <= (SIZE-1); pass++)
//for every 2 array elements comparison do
//the comparison and swap…
for(i = 0; i <= (SIZE-2); i++)
//set the condition…
if(a[i]> a[i + 1])
{
//put the a[i]in temporary variable hold…
hold = a[i];
//put the a[i + 1]in a[i] a[i]= a[i + 1];
//put the hold in a[i + 1], one swapping is
//completed…and repeats for other elements…
a[i + 1]= hold;
}

printf(“\n\nData items in ascending order\n\n”);
//display the new ordered list…
for(i=0; i <= (SIZE-1); i++)
printf(“M”, a[i]);
printf(“\n\n”);
return 0;
}

—————————————————————————————————

//The iostream(.h) header file actually for Standard C++…in Microsoft and Borland it
//is used during the standard development…so used for C++…not in C…
//in C use stdio.h….read more in http://www.tenouk.com/Module23.html
#include
#include
#include

int main(int argc, char *argv[])
{
//reserve 5 byte of buffer….
//should allocate 8 bytes = 2 double words,
//to overflow, need more than 8 bytes…
//so, if more than 8 characters input by user,
//there will be access violation, segmentation fault etc
char mybuffer[5];
//a prompt how to execute the program…
if(argc < 2)
{
printf(“strcpy() NOT executed….\n”);
printf(“Syntax: %s \n”, argv[0]);
exit(0);
}

//copy the user input to mybuffer…
strcpy(mybuffer, argv[1]);
printf(“mybuffer content= %s\n”, mybuffer);
printf(“strcpy() executed…\n”);
return 0;
}

———————————-GCC on Linux/Fedora—————————————

/************array3.c**************/
/*program to find the smallest number in an array named balance*/
/*simple search function*/
#include
#define n 7

int main()
{
int i;
int small, balance[n];

/**loop for displaying array content….*/
for(i=0; i<=n; i++)
{
printf(“Key in float value, let me … for you: “);
scanf(“%d”, &balance[i]);
}

/*printing the element…*/
for(i=0; i<=n; i++)
printf(“%d “, balance[i]);

small = balance[0];
/*Another loop do the array element comparing…*/
for(i=1; i<=n; i++) /*check until i=n*/
{
if(small > balance[i])
small = balance[i];
}

printf(“\nComparing…”);
/*display the result…*/
printf(“The smallest value in the given array is = %d \n”, small);
return 0;
}

————————————————————————————-

/************array1.c*****************/
/*Simple sorting program that sort a list of n*/
/*integer numbers, entered by the user (ascending)*/
#include
#define maxsize 100

int main()
{
int temp, i, j, n, list[maxsize];

printf(“\n–You are prompted to enter your list size.–“);
printf(“\n–Then, for your list size, you are prompted to enter–“);
printf(“\n–the element of your list.–“);
printf(“\n–Finally your list will be sorted ascending–\n”);

/*get the list size…*/
printf(“\nEnter your list size: “);
scanf(” %d”, &n);

/*prompting the data from user store in the list…*/
for(i=0; i {
printf(“Enter list’s element #%d –>”, i);
scanf(“%d”, &list[i]);
}

//do the sorting…
for(i=0; i for(j=i+1; j if(list[i]> list[j])
{
/*These three lines swap the elements*/
/*list[i]and list[j].*/
temp = list[i];
list[i]= list[j];
list[j]= temp;
}
printf(“\nSorted list, ascending: “);

for(i=0; i printf(” %d”, list[i]);
printf(“\n”);
return 0;
}

————————————————————————————-

/**********************array2.c********************/
/*Printing 3×3 array’s subscript and their element*/
#include
#define m 3
#define n 3

int main()
{
int i, j;
int x[m][n];

printf(“\n3x3 arrays’ subscripts and\n”);
printf(“their respective elements\n”);
printf(“————————–\n”);

for(i=0; i for(j=0; j {
printf(“Enter int values for ur array lol!: “);
scanf(“%d”, &x[i][j]);
}
/*outer for loop, reading the row by row…*/
for(i=0; i /*inner loop, for every row, read every column by column…*/
for(j=0; j printf(“x[%d][%d] = %d\n”, i, j, x[i][j]);
return 0;
}

=====================================================================================================;>;>;>;>.h>;>;>;>;>.h>.h>.h>.h>.h>;
[i];
;>;>;>;>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>;
[i][j];
;>;>;>;>;>.h>.h><<“\n”;
;
;>;>.h>.h>.h><<
<<“][“<;>;>.h>.h>;>;>.h>.h>[i]<<“>;>.h>.h>.h>.h>

  ;>;>;>;>.h>;>;>;>;>.h>.h>.h>.h>.h>;
[i];
;>;>;>;>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>.h>;
[i][j];
;>;>;>;>;>.h>.h><<“\n”;
;
;>;>.h>.h>.h><<

<<“][“<;>;>.h>.h>;>;>.h>.h>[i]<<“>;>.h>.h>.h>.h>

Share.

About Author

Leave A Reply