=================================MODULE7=================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #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 #include #define n 7 //function prototype int get_total(int*, int); int main() { int total, y[n]={6,9,2,4,5,23,12}; cout<<"\nCalling function get_total(y, n),"; cout<<"\nBy bringing along the value of y, an array"; cout<<"\nfirst address and n = 7, an array size."; cout<<"\nAn array name, is the pointer to the"; cout<<"\n1st element of an array\n\n"; //function call, pass along the pointer to the first //array element and the array size, and the //return result assign to variable total total = get_total(y, n); cout<<"\nSum of the 7 array elements is "< #define n 7 using namespace std; //function prototype int get_total(int*, int); int main() { int total, y[n]={6,9,2,4,5,23,12}; cout<<"\nCalling function get_total(y, n),"; cout<<"\nBy bringing along the value of y, an array"; cout<<"\nfirst address and n = 7, an array size."; cout<<"\nAn array name, is the pointer to the"; cout<<"\n1st element of an array\n\n"; //function call, pass along the pointer to the first //array element and the array size, and the //return result assign to variable total total = get_total(y, n); cout<<"\nSum of the 7 array elements is "< #include #define n 10 int main() { int i; float small, balance[n]={100.00,40.00,-30.00,400.00,60.00,-25.00,-24.00,0.00, 3.24,0.50}; small = balance[0]; //loop for displaying array content.... for(i=0; i balance[i]) small = balance[i]; } cout<<"\nSearching..."< #include #define maxsize 100 int main() { int temp, i, j, n, list[maxsize]; cout<<"\n--You are prompted to enter your list size.--"; cout<<"\n--Then, for your list size, you are prompted to enter--"; cout<<"\n--the element of your list.--"; cout<<"\n--Finally your list will be sorted ascending!!!--\n"; //get the list size... cout<<"\nEnter your list size: "; cin>>n; //prompting the data from user store in the list... for(i=0; i"; cin>>list[i]; } //do the sorting... for(i=0; i list[j]) { //These three lines swap the elements //list[i] and list[j]. temp = list[i]; list[i] = list[j]; list[j] = temp; } cout<<"\nSorted list, ascending: "; for(i=0; i #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 #include #define m 4 #define n 5 int main() { int i, j, total = 0; //4x5 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 #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; i0) //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 = "< #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 #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("%s%13s%17s\n","Element/index", "Value", "Histogram"); //do the iteration... //outer for loop, read row by row... for(i=0; i <= (SIZE-1); i++) { printf("%9d%15d ", 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("%4d", 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>name[i]; } //inner for loop, read row by row set outer for loop... for(i=0; i0) { //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 #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("%4d", 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", i); scanf("%d", &list[i]); } //do the sorting... for(i=0; 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 #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