lab. programs in c

103
[Programming in C of College days ] Programs are tested with compiler and respective IDEs Bloodshed-DevC++, Visual Studio 2008, Qt 4.2. These are running successfully with console in windows platform. So just enjoy coding. For Visual Studio 2008 and Qt 4.2, please notice the note at the end. 2012 Saket Kumar Pathak Software Developer 3D Graphics

Upload: saket-pathak

Post on 13-Jan-2015

1.917 views

Category:

Education


3 download

DESCRIPTION

A few Programs, that are tested with compilers of respective IDEs s Bloodshed-DevC++, Visual Studio 2008, Qt 4.2. These are running successfully within console window of windows platform. So just enjoy coding. Programs in C of College days.

TRANSCRIPT

Page 1: Lab. Programs in C

[ ]Programs are tested with compiler and respective IDEs Bloodshed-DevC++, Visual Studio 2008, Qt 4.2. These are running successfully with console in windows platform. So just enjoy coding. For Visual Studio 2008 and Qt 4.2, please notice the note at the end.

2012

Saket Kumar Pathak Software Developer 3D Graphics

Page 2: Lab. Programs in C

Programming in C of College days

1. WAP to converta. Binary to Decimalb. Decimal to Binary

Program:Binary to Decimal:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>

#define DATA_SIZE 32

char* rev_data_seq(char* chp_data_seq);

void bin_to_dec(){

printf("WAP to convert :\n\t-> Binary to Decimal");printf("\nLimitation: \n\t-> Fractional Numbers will not considered. \n\t-> Binary format is limited to 32 bit. \n\t-> Input will considered in Unsigned format.");printf("\n\n\n");

int i_res, i_num, i_count;char ch_digit;char* chp_data_seq = (char*)malloc(sizeof(char) *

DATA_SIZE);

i_res = i_num = i_count = 0;

printf("Please Enter any number (Binary in base 2): ");

while((ch_digit = getchar()) != '\n'){

if((ch_digit == '0')||(ch_digit == '1')){

*(chp_data_seq + i_count) = ch_digit;i_count++;

}else if(i_count > 32){

i_res = 0;printf("\nInvalid Input.\n");break;

}else

M.Tech – CSE – Weekend (2012 – 2015) Page 2

Page 3: Lab. Programs in C

Programming in C of College days

{i_res = 0;printf("\nInvalid Input.\n");break;

}}*(chp_data_seq + i_count) = '\0';

chp_data_seq = rev_data_seq(chp_data_seq);

i_count = 0;while (*(chp_data_seq + i_count) != '\0'){

ch_digit = *(chp_data_seq + i_count);if(ch_digit == '0')

i_num = (int)(0 * pow(2.0f, i_count));else if(ch_digit == '1')

i_num = (int)(1 * pow(2.0f, i_count));i_res += i_num;i_count++;

}

printf("\nResultant: %d", i_res);}

char* rev_data_seq(char* chp_data_seq){

char* temp_seq = (char*)malloc(sizeof(char) * (strlen(chp_data_seq) + 1));

int i_count = 0;int i_rev = (strlen(chp_data_seq)-1);while (*(chp_data_seq + i_count) != '\0'){

*(temp_seq + i_rev) = *(chp_data_seq + i_count);i_count++;i_rev--;

}

*(temp_seq + i_count) = '\0';

return temp_seq;}

M.Tech – CSE – Weekend (2012 – 2015) Page 3

Page 4: Lab. Programs in C

Programming in C of College days

int main(){ dec_to_bin(); getch(); return 0;}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

Decimal to Binary:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>

#define DATA_SIZE 32

void dec_to_bin(){

printf("WAP to convert :\n\t-> Decimal to Binary");printf("\nLimitation: \n\t-> Fractional Numbers will not considered. \n\t-> Binary format is limited to 32 bit. \n\t-> Generated result will be in Unsigned format.");printf("\n\n\n");

int i_num;printf("Please Enter any number (Decimal in base 10): ");scanf("%d", &i_num);

int* i_res = (int*)malloc(sizeof(int) * DATA_SIZE);int i_count = 0;while(i_count < DATA_SIZE){

*(i_res + i_count) = 0;i_count++;

}

printf("\n");i_count = (DATA_SIZE-1);int i_quotent, i_remainder;do{

M.Tech – CSE – Weekend (2012 – 2015) Page 4

Page 5: Lab. Programs in C

Programming in C of College days

i_quotent = i_num / 2;i_remainder = i_num % 2;i_num = i_quotent;*(i_res + i_count) = i_remainder;i_count--;

}while(i_quotent > 0);

printf("\nResultant: ");i_count = 0;while(i_count < DATA_SIZE){

printf("%d", *(i_res + i_count));i_count++;

}}

int main(){ dec_to_bin(); getch(); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 5

Page 6: Lab. Programs in C

Programming in C of College days

2. WAP to generate prime numbers within a given range.

Program:

#include <stdio.h>#include <stdbool.h>

int i_lr, i_hr, i_count_prime;int *i_collector;bool b_check;

void set_argument(){ printf("Please Enter Number to for lower limit of Range: "); scanf("%d", &i_lr); printf("Please Enter Number to for Upper limit of Range: "); scanf("%d", &i_hr);

printf("\n\n\n");}

void find_numbers(){ printf(" Given Range to find prime numbers is %d - %d.\n", i_lr, i_hr);

i_collector = (int*)malloc(sizeof(int) * 1024); *i_collector = 1;

{ int i_count; for(i_count = i_lr; i_count <= i_hr; ++i_count) { if(i_count > 3) { int i_loopcount;for(i_loopcount = 2; i_loopcount <= (i_count/2); ++i_loopcount) { if((i_count % i_loopcount) == 0) { b_check = true; break; } else b_check = false;

M.Tech – CSE – Weekend (2012 – 2015) Page 6

Page 7: Lab. Programs in C

Programming in C of College days

} if(!b_check) { *i_collector = i_count; i_collector+=sizeof(int); i_count_prime++; } } else { *i_collector = i_count; i_collector+=sizeof(int); printf("\n The Number 2 and 3 are prime."); } } } printf("\n\n\n");}

void show_result(){ i_collector-=sizeof(int);

printf("Prime Number So obtained: \n"); printf("Index \t PrimeNumber\n");

int i_idx = 1; while(i_count_prime > 0) { printf(" %d\t %d\n", i_idx, *i_collector); i_collector = i_collector - sizeof(int); --i_count_prime; ++i_idx; }}int main(){ printf("WAP to Find Prime numbers with a given range."); printf("\n\n\n"); set_argument(); find_numbers(); show_result(); printf("\n\n\n"); system("pause"); return 0;

M.Tech – CSE – Weekend (2012 – 2015) Page 7

Page 8: Lab. Programs in C

Programming in C of College days

}

3. WAP to generate following patterns, if line =5(a)

10 11 0 10 1 0 11 0 1 0 1

(b)

** * *

* * * * ** * * * * * *

* * * * * * * * *

Program:

(a)

#include <stdio.h>#include <stdbool.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t1\n"); printf("\t01\n"); printf("\t101\n"); printf("\t0101\n"); printf("\t10101\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 8

Page 9: Lab. Programs in C

Programming in C of College days

}

void draw_pattern(){ int container[2] = {1,0};

unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= (unsigned int)i_number; ++ui_row) { printf("\n\t"); int count; bool b_flag = true; for(ui_col = 1; ui_col <= ui_row; ++ui_col) { if(b_flag) { if((ui_row%2)==0) count = 1; else count = 0; b_flag = false; }

printf("%d", container[count]);

if(count==1) count = 0; else count = 1; } }}

int main(){ set_argument(); draw_pattern(); getch(); return 0;}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

M.Tech – CSE – Weekend (2012 – 2015) Page 9

Page 10: Lab. Programs in C

Programming in C of College days

(b)

#include <stdio.h>#include <stdbool.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t *\n"); printf("\t ***\n"); printf("\t *****\n"); printf("\t *******\n"); printf("\t*********\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_blank, ui_col; for(ui_row = 0; ui_row <= (unsigned int)i_number; ++ui_row) { printf("\n\t"); for(ui_blank = 1; ui_blank <= (i_number-ui_row); ++ui_blank) printf(" ");

for(ui_col = 1; ui_col <= (2*ui_row + 1); ++ui_col) printf("*"); }}

M.Tech – CSE – Weekend (2012 – 2015) Page 10

Page 11: Lab. Programs in C

Programming in C of College days

int main(){ set_argument(); draw_pattern(); getch(); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 11

Page 12: Lab. Programs in C

Programming in C of College days

4. WAP to generate recursive Fibonacci series.

Program:

#include <stdio.h>#include <stdbool.h>

void create_fibonacci(bool flag, unsigned int i_count);

int main(){ printf("WAP to generate Fibonacci series using recursion.\n"); printf("\n\n\n"); int i_term, i_rec; unsigned int ui_count; printf("Enter a term to create fibonacci: "); scanf("%d", &i_term); printf("Fibonacci: 0 1"); create_fibonacci(true, (i_term-2)); system("pause"); return 0;}

void create_fibonacci(bool flag, unsigned int i_count){ static int i_num_1, i_num_2, i_num_3; if (flag) { i_num_1 = 0; i_num_2 = 1; flag = false; } if (i_count > 0) { i_num_3 = i_num_1 + i_num_2; printf(" %d", i_num_3);

M.Tech – CSE – Weekend (2012 – 2015) Page 12

Page 13: Lab. Programs in C

Programming in C of College days

i_num_1 = i_num_2; i_num_2 = i_num_3; create_fibonacci(false, --i_count); } printf("\n");}

M.Tech – CSE – Weekend (2012 – 2015) Page 13

Page 14: Lab. Programs in C

Programming in C of College days

5. WAP to perform binary search by using a. Recursion.b. Non recursion.

Program:

a. Recursion

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <math.h>

bool check_item_order(int* i_storage){ //Function to check the Item - List is in Sorted order.

return true;}

void recursive_bin_search(int* i_storage, int i_srch_item, int i_low, int i_mid, int i_hi){

if(i_low < i_hi){

if(i_storage[i_mid] == i_srch_item){

printf("Item (%d) is found at position: %d", i_srch_item, i_mid);}else if(i_storage[i_mid] > i_srch_item){

i_hi = i_mid;i_mid = (i_low + i_hi)/2;

recursive_bin_search(i_storage, i_srch_item, i_low, i_mid, i_hi);}else if(i_storage[i_mid] < i_srch_item){

i_low = i_mid;i_mid = (i_low + i_hi)/2;

recursive_bin_search(i_storage, i_srch_item, i_low, i_mid, i_hi);}

}

M.Tech – CSE – Weekend (2012 – 2015) Page 14

Page 15: Lab. Programs in C

Programming in C of College days

}

void recursive_binary_search(int* i_storage, int i_num_item){

bool b_check = check_item_order(i_storage);

printf("Your Storage Items are: \n\t");

int i_loop_count;for(i_loop_count = 0; i_loop_count < i_num_item; +

+i_loop_count){

printf("%d, ", i_storage[i_loop_count]);}

printf("\n\n\n");

int i_srch_item;printf("Please Enter the item (number) to search: ");scanf("%d", &i_srch_item);

printf("\n\n\n");int i_low = 0;int i_hi = i_num_item;int i_mid = (i_low + i_hi)/2;

if(b_check)recursive_bin_search(i_storage, i_srch_item, i_low,

i_mid, i_hi);else

printf("\nItems are not in Order.\n");}

void binary_search(void){

printf("WAP for Binary - Search.");printf("\nLimitation: \n\t-> Items are restrickted with integer number.\n\t-> Starting index of storage is 0.");

printf("\n\n\n");

int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

recursive_binary_search(i_storage, (sizeof(i_storage)/sizeof(int)));}

M.Tech – CSE – Weekend (2012 – 2015) Page 15

Page 16: Lab. Programs in C

Programming in C of College days

int main(){

binary_search();

printf("\n\n\n");getch();return 0;

}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

b. Non recursion

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <math.h>

bool check_item_order(int* i_storage){

return true;}

void iterative_binary_search(int* i_storage, int i_num_item){

bool b_check = check_item_order(i_storage);

printf("Your Storage Items are: \n\t");

int i_loop_count;for(i_loop_count = 0; i_loop_count < i_num_item; +

+i_loop_count){

printf("%d, ", i_storage[i_loop_count]);}

printf("\n\n\n");

int i_srch_item;printf("Please Enter the item (number) to search: ");

M.Tech – CSE – Weekend (2012 – 2015) Page 16

Page 17: Lab. Programs in C

Programming in C of College days

scanf("%d", &i_srch_item);

printf("\n\n\n");

printf("Result: ");

int i_low = 0;int i_hi = i_num_item;int i_mid = (i_low + i_hi)/2;if (b_check){

while(i_low < i_hi){

if(i_storage[i_mid] == i_srch_item){

printf("Item (%d) is found at position: %d", i_srch_item, i_mid);break;

}else if(i_storage[i_mid] > i_srch_item){

i_hi = i_mid;i_mid = (i_low + i_hi)/2;

}else if(i_storage[i_mid] < i_srch_item){

i_low = i_mid;i_mid = (i_low + i_hi)/2;

}}

}}

void binary_search(void){

printf("WAP for Binary - Search.");printf("\nLimitation: \n\t-> Items are restrickted with integer number.\n\t-> Starting index of storage is 0.");

printf("\n\n\n");

int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

iterative_binary_search(i_storage, (sizeof(i_storage)/sizeof(int)));}

M.Tech – CSE – Weekend (2012 – 2015) Page 17

Page 18: Lab. Programs in C

Programming in C of College days

int main(){

binary_search();

printf("\n\n\n");getch();return 0;

}

M.Tech – CSE – Weekend (2012 – 2015) Page 18

Page 19: Lab. Programs in C

Programming in C of College days

6. WAP for Tower of Hanoi. Also draw the recursive tree for n=4 disks.

Program:

#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#include <math.h>

void generate_moves_toh(int i_disk_num, char from_peg, char to_peg, char aux_peg){

/* If only 1 disk, make the move and return */if(i_disk_num == 1){

printf("\nMove disk 1 from peg %c to peg %c", from_peg, to_peg);

return;}

/* Move top n-1 disks from A to B, using C as auxiliary */generate_moves_toh(i_disk_num-1, from_peg, aux_peg, to_peg);

printf("\nMove disk %d from peg %c to peg %c", i_disk_num, from_peg, to_peg);

/* Move n-1 disks from B to C using A as auxiliary */generate_moves_toh(i_disk_num-1, aux_peg, to_peg, from_peg);

}

void gen_tow_of_honoi(){ printf("WAP to display the steps required in solving 'Tower of Hanoi' for 'n' number of disks.");

printf("\nLimitation: \n\t-> Disks are represented as integral numbers in assending order.");

printf("\n\n\n");

int i_num_disk; printf("Enter the number of disks : "); scanf("%d",&i_num_disk); printf("The Tower of Hanoi involves the moves :\n\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 19

Page 20: Lab. Programs in C

Programming in C of College days

generate_moves_toh(i_num_disk, 'A', 'C', 'B');}

int main(){

gen_tow_of_honoi();

printf("\n\n\n");getch();

return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 20

Page 21: Lab. Programs in C

Programming in C of College days

7. WAP to generate Pascal triangle viz. if line =511 11 2 11 3 3 11 4 6 4 1

Program:

#include <stdio.h>#include <stdbool.h>

int main(){ printf("Pascal's Series\n"); printf("\n\n\n"); int i_num_row; printf("Enter number of rows: "); scanf("%d", &i_num_row); int arr[6], arr_temp[6]; static bool flag = true; int i_row_count, i_col_count, i_count, i_counter; for (i_row_count = 1; i_row_count <= i_num_row; ++i_row_count) { for (i_col_count = 1; i_col_count <= i_num_row; ++i_col_count) { for (i_count = 0; i_count <= i_row_count; ++i_count) { if (flag) { arr[i_count] = i_count; if (arr[i_count] != 0) printf("\t%d", arr[i_count]); } else if(i_count > 0) {

M.Tech – CSE – Weekend (2012 – 2015) Page 21

Page 22: Lab. Programs in C

Programming in C of College days

arr_temp[i_count] = arr[i_count-1] + arr[i_count]; printf("\t%d", arr_temp[i_count]); } else { arr_temp[i_count] = i_count; } }for (i_counter = i_count; i_counter <= (i_num_row+1); ++i_counter) { if (flag) { arr[i_counter] = 0; } else if(i_counter > 0) { arr_temp[i_counter] = arr[i_counter-1] + arr[i_counter]; } } flag = false; break; } if (i_row_count > 1) for (i_count = 1; i_count <= i_num_row; ++i_count) { arr[i_count] = arr_temp[i_count]; } printf("\n"); } system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 22

Page 23: Lab. Programs in C

Programming in C of College days

8. Write following programs by using passing array to function :a) Bubble sortb) Insertion sortc) Selection sort

Program:

a) Bubble sort

#include <stdio.h>

int* bubble_sort(int i_store[], int i_size);int* get_elem(int i_size);void disp_elem(int i_store[], int i_size);

int main(){ printf("WAP of Bubble sort by using array."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = bubble_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("\n\n\n"); system("pause"); return 0;}

int* get_elem(int i_size){ int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("\nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++;

M.Tech – CSE – Weekend (2012 – 2015) Page 23

Page 24: Lab. Programs in C

Programming in C of College days

i_size--; } return ip_store;}

int* bubble_sort(int i_store[], int i_size){ int i_temp; int i_count_0, i_count_1; for (i_count_0 = (i_size - 1); i_count_0 > 0; --i_count_0) { for (i_count_1 = 1; i_count_1 <= i_count_0; ++i_count_1) { if (i_store[i_count_1 - 1] > i_store[i_count_1]) { i_temp = i_store[i_count_1 - 1]; i_store[i_count_1 - 1] = i_store[i_count_1]; i_store[i_count_1] = i_temp; } } } return i_store;}

void disp_elem(int i_store[], int i_size){ printf("\nDisplaying Elements of store: \n"); int i_ndx = 0; while (i_size) { printf("\nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; }}

M.Tech – CSE – Weekend (2012 – 2015) Page 24

Page 25: Lab. Programs in C

Programming in C of College days

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

Insertion sort

#include <stdio.h>

int* insertion_sort(int i_store[], int i_size);int* get_elem(int i_size);void disp_elem(int i_store[], int i_size);

int main(){ printf("WAP of Insertion sort by using array."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = insertion_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("\n\n\n"); system("pause"); return 0;}

int* get_elem(int i_size){ int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("\nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store;}

int* insertion_sort(int i_store[], int i_size){

M.Tech – CSE – Weekend (2012 – 2015) Page 25

Page 26: Lab. Programs in C

Programming in C of College days

int i_temp; int i_count_0, i_count_1, i_count_2; for (i_count_0 = 1; i_count_0 <= (i_size-1); ++i_count_0) { for (i_count_1 = 0; i_count_1 < i_count_0; ++i_count_1)

{if (i_store[i_count_1] > i_store[i_count_0]){

i_temp = i_store[i_count_1] ;i_store[i_count_1] = i_store[i_count_0] ;

for (i_count_2 = i_count_0; i_count_2 > i_count_1; i_count_2--)

i_store[i_count_2] = i_store[i_count_2 - 1] ;

i_store[i_count_2 + 1] = i_temp ;}

} } return i_store;}

void disp_elem(int i_store[], int i_size){ printf("\nDisplaying Elements of store: \n"); int i_ndx = 0; while (i_size) { printf("\nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; }}

M.Tech – CSE – Weekend (2012 – 2015) Page 26

Page 27: Lab. Programs in C

Programming in C of College days

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

Selection sort

#include <stdio.h>

int* selection_sort(int i_store[], int i_size);int* get_elem(int i_size);void disp_elem(int i_store[], int i_size);

int main(){ printf("WAP of Selection sort by using array."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = selection_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("\n\n\n"); system("pause"); return 0;}

int* get_elem(int i_size){ int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("\nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store;}

int* selection_sort(int i_store[], int i_size)

M.Tech – CSE – Weekend (2012 – 2015) Page 27

Page 28: Lab. Programs in C

Programming in C of College days

{ int i_temp; int i_count_0, i_count_1; for (i_count_0 = 0; i_count_0 < (i_size-1); ++i_count_0) { for (i_count_1 = (i_count_0+1); i_count_1 < i_size; ++i_count_1) { if (i_store[i_count_0] > i_store[i_count_1]) { i_temp = i_store[i_count_0]; i_store[i_count_0] = i_store[i_count_1]; i_store[i_count_1] = i_temp; } } } return i_store;}

void disp_elem(int i_store[], int i_size){ printf("\nDisplaying Elements of store: \n"); int i_ndx = 0; while (i_size) { printf("\nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; }}

M.Tech – CSE – Weekend (2012 – 2015) Page 28

Page 29: Lab. Programs in C

Programming in C of College days

9. WAP for matrix multiplication of given order.

Program:

#include <stdio.h>#include <stdbool.h>

void get_matrices(void);void mult_matrices(void);void disp_matrix(void);

int **mat_1;int **mat_2;int **res_mat;

int i_m1_row, i_m1_col;int i_m2_row, i_m2_col;

int main(){ printf("WAP for matrix multiplication of given order.\n"); printf("\n\n\n"); get_matrices(); mult_matrices(); disp_matrix(); system("pause"); return 0;}

void get_matrices(){ printf("Enter number of rows for Matrix-1: "); scanf("%d", &i_m1_row); printf("Enter number of columbs for Matrix-1: "); scanf("%d", &i_m1_col); mat_1 = malloc(sizeof(int) * i_m1_row); int i_row_count, i_col_count; printf("Enter elements for Matrix-1:\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 29

Page 30: Lab. Programs in C

Programming in C of College days

for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { *(mat_1+i_row_count) = malloc(sizeof(int) * i_m1_col); for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d]: ", i_row_count, i_col_count); scanf("%d",&(*(*(mat_1+i_row_count)+i_col_count))); *(mat_1+i_row_count)+i_col_count; } } printf("\n\n\n"); printf("Enter number of rows for Matrix-2: "); scanf("%d", &i_m2_row); printf("Enter number of columbs for Matrix-2: "); scanf("%d", &i_m2_col); mat_2 = malloc(sizeof(int) * i_m2_row); printf("Enter elements for Matrix-2:\n"); for (i_row_count = 0; i_row_count < i_m2_row; ++i_row_count) { *(mat_2+i_row_count) = malloc(sizeof(int) * i_m2_col); for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { printf("Element[%d][%d]: ", i_row_count, i_col_count); scanf("%d",&(*(*(mat_2+i_row_count)+i_col_count))); *(mat_2+i_row_count)+i_col_count; } } printf("\n\n\n"); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d] - Mat-1: %d\n", i_row_count, i_col_count, mat_1[i_row_count][i_col_count]); } }

M.Tech – CSE – Weekend (2012 – 2015) Page 30

Page 31: Lab. Programs in C

Programming in C of College days

printf("\n\n\n"); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d] - Mat-2: %d\n", i_row_count, i_col_count, mat_2[i_row_count][i_col_count]); } } printf("\n");}

void mult_matrices(void){ printf("\n\n\n"); int i_row_count, i_col_count, i_count; res_mat = malloc(sizeof(int) * i_m1_row);

printf("Resultant Matrix: Row- %d\t Column- %d\n", i_m1_row, i_m2_col); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { *(res_mat+i_row_count) = malloc(sizeof(int) * i_m2_col); for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { *(*(res_mat+i_row_count)+i_col_count) = 0; for (i_count = 0; i_count < i_m1_row; ++i_count) { *(*(res_mat+i_row_count)+i_col_count) += (*(*(mat_1 + i_row_count)+i_count)) * (*(*(mat_2 + i_count)+i_col_count)); } } } printf("\n\n\n");}

void disp_matrix(void){

M.Tech – CSE – Weekend (2012 – 2015) Page 31

Page 32: Lab. Programs in C

Programming in C of College days

int i_row_count, i_col_count; for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { printf("Resultant[%d][%d] - Res-2: %d\n", i_row_count, i_col_count, *(*(res_mat+i_row_count)+i_col_count)); } } printf("\n\n\n");}

10. WAP without using string’s function for :a. Copy of one string in anotherb. Concatenation of string

M.Tech – CSE – Weekend (2012 – 2015) Page 32

Page 33: Lab. Programs in C

Programming in C of College days

c. Comparison of stringsd. Reverse the string

Program:

a. Copy of one string in another

#include <stdio.h>

#define STR_SIZE 1024

char* get_string(void);char* str_copy(char* gvn_str);

int main(){ printf("WAP without using string's function to Copy one string into another."); printf("\n\n\n"); char* temp = malloc(sizeof(char) * STR_SIZE); temp = get_string(); printf("\n\n\n"); printf("Copied String: %s", str_copy(temp)); printf("\n\n\n"); system("pause"); return 0;}

char* get_string(void){ char* chp_var1; char ch_temp; int i_num = 0; chp_var1 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string to copy ('\\n' is terminating character): "); while ((ch_temp = getchar()) != '\n') { *(chp_var1 + i_num) = ch_temp; i_num++; }

M.Tech – CSE – Weekend (2012 – 2015) Page 33

Page 34: Lab. Programs in C

Programming in C of College days

*(chp_var1 + i_num) = '\0'; return chp_var1;}

char* str_copy(char* gvn_str){ char* chp_var; int i_num = 0; chp_var = malloc(sizeof(char) * STR_SIZE); while (*(gvn_str + i_num) != '\0') { *(chp_var + i_num) = *(gvn_str + i_num); i_num++; } *(chp_var + i_num) = '\0'; return chp_var;}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

b. Concatenation of string

#include <stdio.h>

#define STR_SIZE 1024

char* concat_str(char* chp_var1, char* chp_var2);unsigned int get_str_len(char* chp_var);

int main(){ printf("WAP without using string's function for Concatenating of two strings. "); char* chp_var1; char* chp_var2; char* chp_res; printf("\n\n\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 34

Page 35: Lab. Programs in C

Programming in C of College days

chp_var1 = malloc(sizeof(char) * STR_SIZE); chp_var2 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); printf("\n\n"); printf("Please Enter another string to concatenate: "); scanf("%s", chp_var2); chp_res = concat_str(chp_var1, chp_var2); printf("\nResultant String (after Copying): %s", chp_res);

printf("\n"); system("pause"); return 0;}

char* concat_str(char* chp_var1, char* chp_var2){ size_t size = get_str_len(chp_var1) + get_str_len(chp_var2);

chp_var1 = (char*)realloc(chp_var1, (size+1)); unsigned int ui_count = 0; while (*(chp_var1+ui_count) != '\0') ui_count++; while (*chp_var2 != '\0') { *(chp_var1+ui_count) = *chp_var2; ui_count++; chp_var2++; } *(chp_var1+ui_count) = '\0'; return chp_var1;}

unsigned int get_str_len(char* chp_var){ unsigned int ui_count = 0; while (*chp_var != '\0')

M.Tech – CSE – Weekend (2012 – 2015) Page 35

Page 36: Lab. Programs in C

Programming in C of College days

{ ui_count++; chp_var++; } return ui_count;}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

c. Comparison of strings

#include <stdio.h>#include <stdbool.h>

#define STR_SIZE 1024

bool comp_str(char* chp_var1, char* chp_var2);

int main(){ printf("WAP without using string's function to Comparison of strings."); char* chp_var1; char* chp_var2; bool b_res; printf("\n\n\n"); chp_var1 = malloc(sizeof(char) * STR_SIZE); chp_var2 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); printf("\n\n"); printf("Please Enter another string to compare: "); scanf("%s", chp_var2); b_res = comp_str(chp_var1, chp_var2); if (b_res) printf("\nStrings are same."); else printf("\nStrings are not same.");

printf("\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 36

Page 37: Lab. Programs in C

Programming in C of College days

system("pause"); return 0;}

bool comp_str(char* chp_var1, char* chp_var2){ unsigned int ui_count = 0; while (*(chp_var1+ui_count)== *(chp_var2+ui_count)) { if (*(chp_var1+ui_count) == '\0') return true; ui_count++; } return false;}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

d. Reverse the string

#include <stdio.h>#include <stdbool.h>

#define STR_SIZE 1024

char* comp_str(char* chp_var1);unsigned int get_str_len(char* chp_var);

int main(){ printf("WAP without using string's function to Reverse the string."); char* chp_var1; char* chp_res; printf("\n\n\n"); chp_var1 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); chp_res = comp_str(chp_var1);

M.Tech – CSE – Weekend (2012 – 2015) Page 37

Page 38: Lab. Programs in C

Programming in C of College days

printf("\nResultant Strings (after reverse): %s", chp_res);

printf("\n"); system("pause"); return 0;}

char* comp_str(char* chp_var1){ size_t size = get_str_len(chp_var1); char* chp_str = malloc(size+1); unsigned int ui_rev_idx = (size - 1); unsigned int ui_count = 0; while (*(chp_var1+ui_count) != '\0') { *(chp_str+ui_rev_idx) = *(chp_var1+ui_count); ui_rev_idx--; ui_count++; } *(chp_str+size) = '\0'; return chp_str;}

unsigned int get_str_len(char* chp_var){ unsigned int ui_count = 0; while (*chp_var != '\0') { ui_count++; chp_var++; } return ui_count;}

M.Tech – CSE – Weekend (2012 – 2015) Page 38

Page 39: Lab. Programs in C

Programming in C of College days

11. WAP to check that given string is palindrome or not?

Program:

#include <stdio.h>#include <stdbool.h>

bool check(char* usr_str, char* chk_str);

int main(){ printf("WAP to check given string is Palindrome.\n"); printf("\n\n\n"); bool b_check; char* chp_var = malloc(sizeof(char) * 1024); char* chk_var = malloc(sizeof(char) * 1024); char* temp = chp_var; unsigned int ui_no_char = 0; printf("Enter string to check palindrome: "); while ((*chp_var = getchar())!='\n') { *chk_var = *chp_var; chk_var++; chp_var++; ui_no_char++; } *chp_var = '\0'; chp_var = temp; chk_var--; b_check = check(chp_var, chk_var); if (b_check) printf("\nResult: %s, is palindrome.\n", chp_var); else printf("\nResult: %s, isn't palindrome.\n", chp_var); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 39

Page 40: Lab. Programs in C

Programming in C of College days

bool check(char* usr_str, char* chk_str){ unsigned int ui_count, ui_chk; ui_chk = ui_count = 0; while (*usr_str != '\0') { ui_count++; if (*usr_str != *chk_str) { return false; } else { ui_chk++; } usr_str++; chk_str--; }

if (ui_count == ui_chk) return true;}

M.Tech – CSE – Weekend (2012 – 2015) Page 40

Page 41: Lab. Programs in C

Programming in C of College days

12. WAP to store the information about book’s Title, Author, Page, & Price. Display the information by using pointer & structure with following restriction :

a. Display elements by passing one element at time.b. By passing all elements at time.

Program:

#include <stdio.h>#include <stdbool.h>#include <ctype.h>#include <string.h>

#define HEAD_SIZE 9#define DIGIT_SIZE 9#define STRING_LEN 30

void get_choice(void);bool store_info(void);bool disp_one_elem(void);bool disp_all_elem(void);char* int_to_alpha(int i_num);

struct bk_detail{ char* chp_stitle; char* chp_sname; int i_spage; int i_sprice;};

int main(){ printf("WAP to store the information about book's Title, Author, Page, & Price. \nDisplay the information by using pointer & structure with following restriction :"); printf("\n\t-> Display elements by passing one element at time. \n\t-> By passing all elements at time."); printf("\n\n\n"); char ch_temp; FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); ch_temp = fgetc(fp); fclose(fp); if (ch_temp == -1)

M.Tech – CSE – Weekend (2012 – 2015) Page 41

Page 42: Lab. Programs in C

Programming in C of College days

{ printf("No record inserted.\n"); get_choice(); } else { printf("\nBook Index File is ready for use.\n"); get_choice(); } fclose(fp); printf("\n\n\n"); system("pause"); return 0;}

char* int_to_alpha(int i_num){ int i_count, i_sign, i_rev_count; char* chp_num = malloc(sizeof(char) * DIGIT_SIZE); char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE); i_sign = 0; i_count = i_rev_count = 0; if (i_num == 0) { *(chp_num + i_count) = '0'; i_count++; } else if (i_num < 0) { i_sign = -1; i_num *= -1; } while (i_num > 0) { *(chp_num + i_count) = i_num % 10 + '0'; i_count++; i_num /= 10; } if (i_sign < 0) { *(chp_num + i_count) = '-'; i_count++; }

M.Tech – CSE – Weekend (2012 – 2015) Page 42

Page 43: Lab. Programs in C

Programming in C of College days

*(chp_num + i_count) = '\0'; i_count = 0; i_rev_count = strlen(chp_num)-1; while (*(chp_num + i_count) != '\0') { *(chp_rev_num + i_count) = *(chp_num + i_rev_count); i_count++; i_rev_count--; } *(chp_rev_num + i_count) = '\0'; return chp_rev_num;}

void get_choice(void){ bool (*pfunc_seq[3])(); pfunc_seq[0] = store_info; pfunc_seq[1] = disp_one_elem; pfunc_seq[2] = disp_all_elem; printf("\nYour choices for operation:\n"); printf("\n\t a. To insert record (press) - 1\n"); printf("\n\t b. To display complete info of the \n\t book (your selected name of book) (press) - 2\n"); printf("\n\t a. To display all records (press) - 3\n"); printf("\nPlease Enter your choice: "); char ch_choice; if ((ch_choice = getchar()) == '1') { (*pfunc_seq[0])(); } else if (ch_choice == '2') { (*pfunc_seq[1])(); } else if (ch_choice == '3') { (*pfunc_seq[2])(); } else printf("\nChoice is invalid.\n"); }

M.Tech – CSE – Weekend (2012 – 2015) Page 43

Page 44: Lab. Programs in C

Programming in C of College days

bool store_info(){ FILE* fp; fp = fopen("book_idx_mgr.txt", "a+"); printf("\nTo store - records.\n"); char* chp_bk_title = malloc(sizeof(char) * STRING_LEN); char* chp_bk_author = malloc(sizeof(char) * STRING_LEN); char* chp_bk_page = malloc(sizeof(char) * STRING_LEN); char* chp_bk_price = malloc(sizeof(char) * STRING_LEN); int i_temp; printf("Please Enter: \n"); printf("\tTitle: "); scanf("%s", chp_bk_title); fputs("Title: ", fp); fputs(chp_bk_title, fp); printf("\tAuthor: "); scanf("%s", chp_bk_author); fputs("\nAuthor: ", fp); fputs(chp_bk_author, fp); printf("\tPage: "); scanf("%d", &i_temp); chp_bk_page = int_to_alpha(i_temp); fputs("\nPage: ", fp); fputs(chp_bk_page, fp); printf("\tPrice(only Integer): "); scanf("%d", &i_temp); chp_bk_price = int_to_alpha(i_temp); fputs("\nPrice: ", fp); fputs(chp_bk_price, fp); char* chp_item_separator = "\n--------------------------------------------\n"; fputs(chp_item_separator, fp); fclose(fp); return true;}

bool disp_one_elem(){

M.Tech – CSE – Weekend (2012 – 2015) Page 44

Page 45: Lab. Programs in C

Programming in C of College days

printf("\nTo display the details of record as per your choice.\n"); struct bk_detail s_bk_detail; s_bk_detail.chp_sname = malloc(sizeof(char) * STRING_LEN); s_bk_detail.chp_stitle = malloc(sizeof(char) * STRING_LEN); s_bk_detail.i_spage = 0; s_bk_detail.i_sprice = 0; char* chp_usr_book = malloc(sizeof(char) * STRING_LEN); printf("Please Enter the title of the book: "); scanf("%s", chp_usr_book); FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); char* chp_str = malloc(sizeof(char) * HEAD_SIZE); char* chp_prev_str = malloc(sizeof(char) * HEAD_SIZE); char ch_temp; int i_ch_count = 0; bool b_store = false; bool b_assign = false; while ((ch_temp = fgetc(fp))!= EOF) { if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp == '_')) { *(chp_str + i_ch_count) = ch_temp; i_ch_count++; } else if ((ch_temp == ':')||(ch_temp == '\n')) { *(chp_str + i_ch_count) = '\0'; i_ch_count = 0; if (ch_temp == ':') { b_store = false; } else if (ch_temp == '\n') b_store = true; if (b_store) { if (strcmp(chp_str, chp_usr_book) == 0) { b_assign = true;

M.Tech – CSE – Weekend (2012 – 2015) Page 45

Page 46: Lab. Programs in C

Programming in C of College days

} if (b_assign) { if (strcmp(chp_prev_str, "Title") == 0) { s_bk_detail.chp_stitle = strcpy(s_bk_detail.chp_stitle, chp_str); printf("\nTitle - %s", s_bk_detail.chp_stitle); } else if (strcmp(chp_prev_str, "Author") == 0) { s_bk_detail.chp_sname = strcpy(s_bk_detail.chp_sname, chp_str); printf("\nAuthor - %s", s_bk_detail.chp_sname); } else if (strcmp(chp_prev_str, "Page") == 0) { int i_temp = atoi(chp_str); s_bk_detail.i_spage = i_temp; printf("\nPage - %d", s_bk_detail.i_spage); } else if (strcmp(chp_prev_str, "Price") == 0) { int i_temp = atoi(chp_str); s_bk_detail.i_sprice = i_temp; printf("\nPrice - %d", s_bk_detail.i_sprice); b_assign = false; } } } else { chp_prev_str = strcpy(chp_prev_str, chp_str); } } } fclose(fp);

M.Tech – CSE – Weekend (2012 – 2015) Page 46

Page 47: Lab. Programs in C

Programming in C of College days

return true;}

bool disp_all_elem(){ printf("\nTo display the details of record.\n"); FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); char* chp_str = malloc(sizeof(char) * HEAD_SIZE); char ch_temp; int i_ch_count = 0; while ((ch_temp = fgetc(fp))!= EOF) { if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp == ':')||(ch_temp == ' ')||(ch_temp == '_')) { *(chp_str + i_ch_count) = ch_temp; i_ch_count++; } else if (ch_temp == '\n') { *(chp_str + i_ch_count) = '\0'; i_ch_count = 0; fputs(chp_str, stdout); fputs("\n", stdout); } } fclose(fp); return true;}

13. WAP to display the sum of entered number by writing on & reading from file.

M.Tech – CSE – Weekend (2012 – 2015) Page 47

Page 48: Lab. Programs in C

Programming in C of College days

Program:

#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <stdbool.h>

#define DIGIT_SIZE 9

void write_file(FILE* fp);void read_file(FILE* fp);

int main(){ printf("WAP to write integer numbers to files, then separate even & odd numbers among them & store & display them in separate files."); printf("\n\n\n"); FILE* fp; printf("Please Note: \n\t->Comma(','), \n\t->Blank-Space (' '), \n\t->Tab ('\\t') \n\t->and Enter ('\\n') are separators"); printf("\n\t->whereas Dot ('.') is terminator, \nLimitation: Program will not accept any number more than 9 digits."); printf("\nPlease use any of spearator before terminator."); printf("\n\n\n"); printf("Please Enter numbers: "); write_file(fp); printf("\n\n\n"); read_file(fp); system("pause"); return 0;}

void write_file(FILE* fp){ fp = fopen("Integer_data.txt", "w+"); int* ip_usr_data; ip_usr_data = malloc(sizeof(int));

M.Tech – CSE – Weekend (2012 – 2015) Page 48

Page 49: Lab. Programs in C

Programming in C of College days

char ch_temp_data; char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; bool b_check = false; while ((ch_temp_data = getchar()) != '.') { if (((ch_temp_data == ',')||(ch_temp_data == ' ')||(ch_temp_data == '\t')||(ch_temp_data == '\n'))&&(b_check)) { b_check = false; *(ch_num + i_count) = '\0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (*ip_usr_data) { fputs(ch_num, fp); fputs(", ", fp); } } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } else if (isalpha(ch_temp_data)) { printf("Invalid input."); } } fclose(fp);}

void read_file(FILE* fp){ fp = fopen("Integer_data.txt", "r+"); FILE* fp_even; fp_even = fopen("Even_data.txt", "w+"); FILE* fp_odd; fp_odd = fopen("Odd_data.txt", "w+"); int* ip_usr_data; ip_usr_data = malloc(sizeof(int));

M.Tech – CSE – Weekend (2012 – 2015) Page 49

Page 50: Lab. Programs in C

Programming in C of College days

char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; bool b_check = false; char ch_temp_data; while ((ch_temp_data = fgetc(fp)) != EOF) { if (((ch_temp_data == ',')||(ch_temp_data == ' ')||(ch_temp_data == '\t')||(ch_temp_data == '\n'))&&(b_check)) { b_check = false; *(ch_num + i_count) = '\0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (*ip_usr_data) { if ((*ip_usr_data % 2) == 0) { fputs(ch_num, fp_even); fputs(", ", fp_even); } else { fputs(ch_num, fp_odd); fputs(", ", fp_odd); }

} } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } } if (fclose(fp_even) == 0) if (fclose(fp_odd) == 0) if (fclose(fp) == 0) printf("Normal termination.\n"); else printf("Abnormal termination.\n"); else printf("Abnormal termination.\n"); else printf("Abnormal termination.\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 50

Page 51: Lab. Programs in C

Programming in C of College days

}

M.Tech – CSE – Weekend (2012 – 2015) Page 51

Page 52: Lab. Programs in C

Programming in C of College days

14. Write integer numbers to files, then separate even & odd numbers among them & store & display them in separate files.

Program:

#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <stdbool.h>

#define DIGIT_SIZE 9

char* int_to_alpha(int i_num);void write_file(FILE* fp, int i_addend, int i_augend);void read_file(FILE* fp);

int main(){ printf("WAP to display the sum of entered number by writing on & reading from file."); printf("\n\n\n"); printf("Limitation: Program will only accept intergral number of size 4 bytes."); printf("\n\n\n"); int i_addend, i_augend; printf("Please Enter numbers: "); scanf("%d", &i_addend); printf("Please Enter numbers: "); scanf("%d", &i_augend); FILE* fp; write_file(fp, i_addend, i_augend); read_file(fp); system("pause"); return 0;}

char* int_to_alpha(int i_num){ int i_count, i_sign, i_rev_count; char* chp_num = malloc(sizeof(char) * DIGIT_SIZE); char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE);

M.Tech – CSE – Weekend (2012 – 2015) Page 52

Page 53: Lab. Programs in C

Programming in C of College days

i_sign = 0; i_count = i_rev_count = 0; if (i_num == 0) { *(chp_num + i_count) = '0'; i_count++; } else if (i_num < 0) { i_sign = -1; i_num *= -1; } while (i_num > 0) { *(chp_num + i_count) = i_num % 10 + '0'; i_count++; i_num /= 10; } if (i_sign < 0) { *(chp_num + i_count) = '-'; i_count++; } *(chp_num + i_count) = '\0'; i_count = 0; i_rev_count = strlen(chp_num)-1; while (*(chp_num + i_count) != '\0') { *(chp_rev_num + i_count) = *(chp_num + i_rev_count); i_count++; i_rev_count--; } *(chp_rev_num + i_count) = '\0'; return chp_rev_num;}

void write_file(FILE* fp, int i_addend, int i_augend){ fp = fopen("Summation.txt", "w+"); char* chp_addend = int_to_alpha(i_addend);

M.Tech – CSE – Weekend (2012 – 2015) Page 53

Page 54: Lab. Programs in C

Programming in C of College days

char* chp_augend = int_to_alpha(i_augend); fputs(chp_addend, fp); fputs(" + ", fp); fputs(chp_augend, fp); fputs(" = ", fp);

fclose(fp);}

void read_file(FILE* fp){ int i_addend, i_augend, i_result; fp = fopen("Summation.txt", "r+"); int* ip_usr_data; ip_usr_data = malloc(sizeof(int)); char* chp_result; char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; int i_sign = 0; bool b_check = false; bool b_flag = false; char ch_temp_data; while ((ch_temp_data = fgetc(fp)) != EOF) { if ((ch_temp_data == ' ')&&(b_check)) { b_check = false; *(ch_num + i_count) = '\0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (i_sign < 0) { *ip_usr_data *= -1; i_sign = 0; } if (*ip_usr_data) { if (b_flag) { i_augend = *ip_usr_data; b_flag = false;

M.Tech – CSE – Weekend (2012 – 2015) Page 54

Page 55: Lab. Programs in C

Programming in C of College days

} else if (!b_flag) { i_addend = *ip_usr_data; } } } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } else if (ch_temp_data == '+') { b_flag = true; } else if (ch_temp_data == '-') { i_sign = -1; } else if (ch_temp_data == '=') { i_result = i_addend + i_augend; printf("Addend: %d\n", i_addend); printf("Augend: %d\n", i_augend); chp_result = int_to_alpha(i_result); printf("Summation-: %s\n", chp_result); } } fputs(chp_result, fp); if (fclose(fp) == 0) printf("Normal termination.\n"); else printf("Abnormal termination.\n"); }

M.Tech – CSE – Weekend (2012 – 2015) Page 55

Page 56: Lab. Programs in C

Programming in C of College days

15. WAP to draw the given pattern as per user value.

***************

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t*\n"); printf("\t**\n"); printf("\t***\n"); printf("\t****\n"); printf("\t*****\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); for(ui_col = 1; ui_col <= ui_row; ++ui_col) printf("*"); } printf("\n\n\n");}

M.Tech – CSE – Weekend (2012 – 2015) Page 56

Page 57: Lab. Programs in C

Programming in C of College days

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 57

Page 58: Lab. Programs in C

Programming in C of College days

16. WAP to draw the given pattern as per user value.

112123123412345

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t1\n"); printf("\t12\n"); printf("\t123\n"); printf("\t1234\n"); printf("\t12345\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); for(ui_col = 1; ui_col <= ui_row; ++ui_col) printf("%d", ui_col); }}

int main(){

M.Tech – CSE – Weekend (2012 – 2015) Page 58

Page 59: Lab. Programs in C

Programming in C of College days

set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 59

Page 60: Lab. Programs in C

Programming in C of College days

17. WAP to draw the given pattern as per user value.

1 21 321 432154321

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t 1\n"); printf("\t 21\n"); printf("\t 321\n"); printf("\t 4321\n"); printf("\t54321\n"); printf("\n");

printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col, ui_count; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col) printf(" ");

unsigned int ui_num_count = (i_number - ui_blank_count);

M.Tech – CSE – Weekend (2012 – 2015) Page 60

Page 61: Lab. Programs in C

Programming in C of College days

for(ui_count = ui_num_count; ui_count >= 1; --ui_count) printf("%d", ui_count); }}

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 61

Page 62: Lab. Programs in C

Programming in C of College days

18. WAP to draw the given pattern as per user value.

554543543254321

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t5\n"); printf("\t54\n"); printf("\t543\n"); printf("\t5432\n"); printf("\t54321\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); int i_count = i_number; for(ui_col = 1; ui_col <= ui_row; ++ui_col, --i_count) printf("%d", i_count); }

M.Tech – CSE – Weekend (2012 – 2015) Page 62

Page 63: Lab. Programs in C

Programming in C of College days

}

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 63

Page 64: Lab. Programs in C

Programming in C of College days

19. WAP to draw the given pattern as per user value.

5 45 345 234512345

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t 5\n"); printf("\t 45\n"); printf("\t 345\n"); printf("\t 2345\n"); printf("\t12345\n"); printf("\n");

printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col, ui_count; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col) printf(" ");

M.Tech – CSE – Weekend (2012 – 2015) Page 64

Page 65: Lab. Programs in C

Programming in C of College days

int i_count = ui_blank_count; for(ui_count = 1; ui_count <= ui_row; ++ui_count, ++i_count) printf("%d", (i_count+1)); }}

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;

}

M.Tech – CSE – Weekend (2012 – 2015) Page 65

Page 66: Lab. Programs in C

Programming in C of College days

20. WAP to draw the given pattern as per user value.

543215432543545

Program:

#include <stdio.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t54321\n"); printf("\t5432\n"); printf("\t543\n"); printf("\t54\n"); printf("\t5\n"); printf("\n");

printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("\n\t"); for(ui_col = i_number; ui_col >= ui_row; --ui_col) printf("%d", ui_col); }

M.Tech – CSE – Weekend (2012 – 2015) Page 66

Page 67: Lab. Programs in C

Programming in C of College days

}

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 67

Page 68: Lab. Programs in C

Programming in C of College days

21. WAP to draw the given pattern as per user value.

5 545 54345 5432345543212345

Program:

#include <stdio.h>#include <stdbool.h>

int i_number;

void set_argument(){ printf("***To Draw The given pattern as per user value.***\n\n\n");

printf("\n"); printf("\t 5\n"); printf("\t 545\n"); printf("\t 54345\n"); printf("\t 5432345\n"); printf("\t543212345\n"); printf("\n");

printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number);

printf("\n\n\n");}

void draw_pattern(){ unsigned int ui_row, ui_col, ui_blank; for(ui_row = 0; ui_row <= i_number; ++ui_row) { printf("\n\t"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_blank = 1; ui_blank <= ui_blank_count; ++ui_blank)

M.Tech – CSE – Weekend (2012 – 2015) Page 68

Page 69: Lab. Programs in C

Programming in C of College days

printf(" ");

bool b_flag_f = true; bool b_flag_b = true;

for(ui_col = (2*ui_row + 1); ui_col >= 1; --ui_col) { if (((ui_col%2)==0)&&(b_flag_f)) { unsigned int ui_count; int i_count = i_number; for(ui_count = 1; ui_count <= (ui_col/2); ++ui_count, --i_count) printf("%d", i_count);

b_flag_f = false; } else if(((ui_col%2)==0)&&(!b_flag_f)&&(b_flag_b)) { unsigned int ui_count; int i_count = i_number - (ui_col/2) +1; for (ui_count = (ui_col/2); ui_count >= 1; --ui_count, ++i_count) printf("%d", i_count);

b_flag_b = false; } } }}

int main(){ set_argument(); draw_pattern(); printf("\n\n\n"); system("pause"); return 0;}

M.Tech – CSE – Weekend (2012 – 2015) Page 69

Page 70: Lab. Programs in C

Programming in C of College days

22. WAP to pass array element one by one using pointer and function.

Program:

#include <stdio.h>

#define SIZE_ARRAY 3

void print_item(int* item);

int main(){ printf("WAP to pass array element one by one using pointer and function.\n"); printf("\n\n\n"); int storage[SIZE_ARRAY][SIZE_ARRAY]; printf("Given order of array is %d * %d\n", SIZE_ARRAY, SIZE_ARRAY); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < SIZE_ARRAY; ++i_loop_count) for (j_loop_count = 0; j_loop_count < SIZE_ARRAY; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &storage[i_loop_count][j_loop_count]); } printf("\n\n\n"); printf("Output as Array-element:\n"); for (i_loop_count = 0; i_loop_count < SIZE_ARRAY; ++i_loop_count) for (j_loop_count = 0; j_loop_count < SIZE_ARRAY; ++j_loop_count) print_item(&storage[i_loop_count][j_loop_count]); printf("\n\n\n"); system("pause");

M.Tech – CSE – Weekend (2012 – 2015) Page 70

Page 71: Lab. Programs in C

Programming in C of College days

return 0;}

void print_item(int* i_item){ static int i_count = 1; printf("%d ", *i_item); if ((i_count%3)==0) printf("\n"); i_count++;}

M.Tech – CSE – Weekend (2012 – 2015) Page 71

Page 72: Lab. Programs in C

Programming in C of College days

23. WAP to find (+)ive, (-)ive, even, odd, prime element from array.

Program:

#include <stdio.h>#include <stdbool.h>

#define SIZE_ARRAY 3

void print_pos_item(int** item, int i_row, int i_col);void print_neg_item(int** item, int i_row, int i_col);void print_evn_item(int** item, int i_row, int i_col);void print_odd_item(int** item, int i_row, int i_col);void print_prime_item(int** item, int i_row, int i_col);

int main(){ printf("WAP to find (+)ive, (-)ive, even, odd, prime element from array.\n"); printf("\n\n\n"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("\nEnter number of Rows: "); scanf("%d", &i_row); printf("\nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: \n\n", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) {

M.Tech – CSE – Weekend (2012 – 2015) Page 72

Page 73: Lab. Programs in C

Programming in C of College days

printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); } printf("\n\n\n"); print_pos_item(ip_storage, i_row, i_col); printf("\n\n\n"); print_neg_item(ip_storage, i_row, i_col); printf("\n\n\n"); print_evn_item(ip_storage, i_row, i_col); printf("\n\n\n"); print_odd_item(ip_storage, i_row, i_col); printf("\n\n\n"); print_prime_item(ip_storage, i_row, i_col); printf("\n\n\n"); system("pause"); return 0;}

void print_pos_item(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; printf("List of (+)ive items:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count) >= 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); }}

void print_neg_item(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count;

M.Tech – CSE – Weekend (2012 – 2015) Page 73

Page 74: Lab. Programs in C

Programming in C of College days

printf("List of (-)ive items:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count) < 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); }}

void print_evn_item(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; printf("List of even items:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if (((*(*(ip_storage + i_loop_count) + j_loop_count)%2) == 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); }}

void print_odd_item(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; printf("List of odd items:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count)%2) != 0) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); }}

M.Tech – CSE – Weekend (2012 – 2015) Page 74

Page 75: Lab. Programs in C

Programming in C of College days

void print_prime_item(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; bool b_check; printf("List of prime items:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { int i_count; for(i_count = 2; i_count <= (i_count/2); ++i_count) { if((*(*(ip_storage + i_loop_count) + j_loop_count) % i_count) == 0) { b_check = true; break; } else b_check = false; } if(!b_check) { printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } }}

M.Tech – CSE – Weekend (2012 – 2015) Page 75

Page 76: Lab. Programs in C

Programming in C of College days

24. WAP to display array element in row-major and column-mjor technique.

Program:

#include <stdio.h>#include <stdbool.h>

void row_major_tech(int** item, int i_row, int i_col);void col_major_tech(int** item, int i_row, int i_col);

int main(){ printf("WAP to display array element in row-major and column-mjor technique.\n"); printf("\n\n\n"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("\nEnter number of Rows: "); scanf("%d", &i_row); printf("\nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: \n\n", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); } printf("\n\n\n"); row_major_tech(ip_storage, i_row, i_col);

M.Tech – CSE – Weekend (2012 – 2015) Page 76

Page 77: Lab. Programs in C

Programming in C of College days

printf("\n\n\n"); col_major_tech(ip_storage, i_row, i_col); printf("\n\n\n"); system("pause"); return 0;}

void row_major_tech(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; printf("List of items as Row-Major technique:\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("\n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } }}

void col_major_tech(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; printf("List of items as Column-Major technique:\n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } }}

M.Tech – CSE – Weekend (2012 – 2015) Page 77

Page 78: Lab. Programs in C

Programming in C of College days

25. WAP to transpose a given matrix using pointer and function.

Program:

#include <stdio.h>#include <stdbool.h>

#define SIZE_ARRAY 3

void print_trans_matrix(int** item, int i_row, int i_col);

int main(){ printf("WAP to transpose a given using pointer and function.\n"); printf("\n\n\n"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("\nEnter number of Rows: "); scanf("%d", &i_row); printf("\nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: \n\n", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); } printf("\n\n\n");

M.Tech – CSE – Weekend (2012 – 2015) Page 78

Page 79: Lab. Programs in C

Programming in C of College days

print_trans_matrix(ip_storage, i_row, i_col); printf("\n\n\n"); system("pause"); return 0;}

void print_trans_matrix(int** ip_storage, int i_row, int i_col){ int i_loop_count, j_loop_count; int** trans_mat = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(trans_mat + i_count) = malloc(sizeof(int) * i_col); printf("\nMatrix You entered:"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("\n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } printf("\nMatrix after Transpose:"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("\n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { *(*(trans_mat + j_loop_count) + i_loop_count) = *(*(ip_storage + i_loop_count) + j_loop_count); printf("%d ", *(*(trans_mat + j_loop_count) + i_loop_count)); } }}

26. WAP to display the mathematical table of given number.

M.Tech – CSE – Weekend (2012 – 2015) Page 79

Page 80: Lab. Programs in C

Programming in C of College days

Program:

#include <stdio.h>

void disp_mult_table_range(int i_lr, int i_hr);

int main(){ printf("WAP to display the mathematical table of given range of numbers.\n"); printf("\n\n\n"); int i_lr, i_hr; printf("Please Enter lower limit: "); scanf("%d", &i_lr); printf("Please Enter higher limit: "); scanf("%d", &i_hr); disp_mult_table_range(i_lr, i_hr); printf("\n\n\n"); system("pause"); return 0;}

void disp_mult_table_range(int i_lr, int i_hr){ int i_multiplicand, i_multiplier; for (i_multiplier = i_lr; i_multiplier <= i_hr; ++i_multiplier) { printf("\n\n\n"); printf("Multiplication Table of %d:\n", i_multiplier); for (i_multiplicand = 1; i_multiplicand <= 10; ++i_multiplicand) { printf("\t%d * %d = %d\n", i_multiplier, i_multiplicand, (i_multiplier * i_multiplicand)); } }}

27. WAP to display the mathematical table of given number.

M.Tech – CSE – Weekend (2012 – 2015) Page 80

Page 81: Lab. Programs in C

Programming in C of College days

Program:

#include <stdio.h>

void disp_mult_table(int i_num);

int main(){ printf("WAP to display the mathematical table of given number.\n"); printf("\n\n\n"); int i_num; printf("Please Enter number: "); scanf("%d", &i_num); disp_mult_table(i_num); printf("\n\n\n"); system("pause"); return 0;}

void disp_mult_table(int i_num){ int i_multiplicand; for (i_multiplicand = 1; i_multiplicand <= 10; ++i_multiplicand) { printf("\t%d * %d = %d\n", i_num, i_multiplicand, (i_num * i_multiplicand)); }}

28. WAP to display alphabets ‘a’ to ‘z’ and “A” to “Z”

M.Tech – CSE – Weekend (2012 – 2015) Page 81

Page 82: Lab. Programs in C

Programming in C of College days

a. Using pointers.b. Without using pointers.

Program:

a. Using pointers.

#include <stdio.h>#include <stdbool.h>

void disp_lower_case_alphabets_wp(void);void disp_upper_case_alphabets_wp(void);

int main(){ printf("WAP to display a to z and A to Z by using pointer.\n"); printf("\n\n\n"); disp_lower_case_alphabets_wp(); printf("\n\n\n"); disp_upper_case_alphabets_wp(); printf("\n\n\n"); system("pause"); return 0;}

void disp_lower_case_alphabets_wp(void){ printf("Displaying lower - case alphabets:-\n"); char* chp_var = "a"; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("\n"); printf("%c\t", *chp_var + ui_loop_count); } }

void disp_upper_case_alphabets_wp(void)

M.Tech – CSE – Weekend (2012 – 2015) Page 82

Page 83: Lab. Programs in C

Programming in C of College days

{ printf("Displaying lower - case alphabets:-\n"); char* chp_var = "A"; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("\n"); printf("%c\t", *chp_var + ui_loop_count); }}

~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

a. Without using pointers.

#include <stdio.h>#include <stdbool.h>

void disp_lower_case_alphabets(void);void disp_upper_case_alphabets(void);

int main(){ printf("WAP to display a to z and A to Z by using pointer.\n"); printf("\n\n\n"); disp_lower_case_alphabets(); printf("\n\n\n"); disp_upper_case_alphabets(); printf("\n\n\n"); system("pause"); return 0;}

void disp_lower_case_alphabets(void){ printf("Displaying lower - case alphabets:-\n"); char chp_var = 'a'; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) {

M.Tech – CSE – Weekend (2012 – 2015) Page 83

Page 84: Lab. Programs in C

Programming in C of College days

if (ui_loop_count%10 == 0) printf("\n"); printf("%c\t", chp_var + ui_loop_count); } }

void disp_upper_case_alphabets(void){ printf("Displaying lower - case alphabets:-\n"); char chp_var = 'A'; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("\n"); printf("%c\t", chp_var + ui_loop_count); }}

M.Tech – CSE – Weekend (2012 – 2015) Page 84

Page 85: Lab. Programs in C

Programming in C of College days

29. WAP to take character sequence (string) and diplay it, using pointer and functions.

Program:

#include <stdio.h>#include <stdbool.h>

#define MAX_SEQL_SIZE 1024

char* get_char_seql(void);void disp_char_seql(char*);

int main(){ printf("WAP to take character sequence (string) and diplay it, using pointer and functions.\n"); printf("\n\n\n"); printf("Please press 'dot'(.) as terminating character."); printf("\n\n\n"); char* chp_seql; chp_seql = get_char_seql(); printf("\n\n\n"); disp_char_seql(chp_seql); printf("\n\n\n"); system("pause"); return 0;}

char* get_char_seql(void){ printf("Enter your character sequel:- "); char* chp_var = (char*)malloc(sizeof(char) * MAX_SEQL_SIZE); char ch_temp; unsigned int ui_count = 0; while((ch_temp = getchar()) != '.') { *(chp_var + ui_count) = ch_temp; ui_count++; } *(chp_var + ui_count) = '\0'; return chp_var;

M.Tech – CSE – Weekend (2012 – 2015) Page 85

Page 86: Lab. Programs in C

Programming in C of College days

}

void disp_char_seql(char* chp_seql){ printf("Character sequel you entered:- %s", chp_seql);}

M.Tech – CSE – Weekend (2012 – 2015) Page 86

Page 87: Lab. Programs in C

Programming in C of College days

Note:

For Visual Studio 2008 and Qt (version- 4.2) you need to format the code as, make all the initialization throughout the function to very initial steps after opening braces.

M.Tech – CSE – Weekend (2012 – 2015) Page 87