Lab Programming in C
Lab Programming in C
#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
#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 { i_quotent = i_num / 2;
M.Tech CSE Weekend (2012 2015) Page 4
Page 5
Page 7
1 0 1
1 0
Page 8
~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~
Page 9
Page 10
Page 11
Page 13
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
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))); }
Page 15
b. Non recursion #include #include #include #include #include <stdio.h> <stdlib.h> <string.h> <stdbool.h> <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: "); scanf("%d", &i_srch_item); printf("\n\n\n");
M.Tech CSE Weekend (2012 2015) Page 16
Page 17
Page 18
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"); generate_moves_toh(i_num_disk, 'A', 'C', 'B');
M.Tech CSE Weekend (2012 2015) Page 19
Page 20
Page 22
~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~
Page 24
~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~
Page 26
Page 28
Page 29
Page 31
Page 32
WAP without using strings function for : Copy of one string in another Concatenation of string Comparison of strings Reverse the string
Page 34
Page 36
Page 37
Page 38
Page 39
Page 40
#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 44
Page 46
Page 47
#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;
M.Tech CSE Weekend (2012 2015) Page 48
Page 51
#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 55
Page 56
Page 57
Page 59
Page 61
Page 62
Page 63
Page 65
Page 66
Page 67
Page 69
Page 70
Page 71
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) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count)));
M.Tech CSE Weekend (2012 2015) Page 72
Page 73
Page 75
Page 77
Page 79
Page 80
Page 81
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); } }
Page 82
Page 84
Page 85
Page 86
Page 87