Computer Assignment: by Akshay Tiwary Xii A 03
Computer Assignment: by Akshay Tiwary Xii A 03
Pointers
Computer Assignment
By Akshay Tiwary XII A 03
Assignment 1
Pointers
1: Pointers Assignment
a) Write a program to accept an integer array and search for an element. The program displays the number of occurrences of the search element. b) Write a program using pointers to read an array of integers and print its elements in reverse order. c) Write a program using pointers which receives a sorted array of integers and an integer value and places the integer value at the correct place. d) Write a function using pointers to add two matrices and returns the pointer to the resultant matrix. e) Write a function using pointers to transpose the matrix passed as parameter and returns a pointer to the transposed matrix. f) Using pointers, write a function that receives a character string and a character as argument and deletes all occurrences of this character in the string. The function should return the corrected string. g) Create a menu based program which emulates the string based functions using only pointers namelya. strcpy() b. strrev() c. strlen() d. strappend() e. strcmp() h) Write a program to create a 2 D array using pointers which stores 5 strings. Sort the array using pointers.
Assignment 1
Pointers
SOURCE CODE:
1.
/* By Akshay Tiwary Date: 30/03/13 This program accepts an integer array and searches for a given element (using pointers). */ #include<iostream> using namespace std; int search(int*,int,int); int main() { int array[10]; cout << "Enter the elements for the array. " << endl; for(int i = 0; i < 10; i++) cin >> array[i]; int elem = 0; cout << "Enter the element to be searched." << endl; cin >> elem; int*ptr = array; int size = 10; int num = search(ptr,elem,size);
Assignment 1
Pointers
cout << "The given element was found " << num << " time(s)." << endl; system("pause"); return 0; } int search(int* arrptr,int elem,int size) { int ctr = 0; for(int i = 0; i < size; i++) if(*(arrptr + i) == elem) { ctr++; } return ctr; }
2.
/* By Akshay Tiwary Date: 30/03/13 This program reads an array of integers and prints its elements in reverse order. */ #include<iostream> using namespace std; int main() { int array[10]; int* ptr = &array[0]; cout << "Enter the elements of the array. Press -1 to stop. Max 10 elements. " << endl;
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
for(int i = 0; i < 10; i++) { int temp = 0; cin >> temp; if(temp != -1) { array[i] = temp; ptr += 1; } else break; } while(ptr-- > array) cout << *ptr << " "; system("pause"); return 0; }
3.
/* By Akshay Tiwary Date: 30/03/13 This program receives a sorted array of integers and an integer value and places the integer value at the correct place. */ #include<iostream> using namespace std; int main() { int array[10]; int* ptr = &array[0]; int ctr = 0;
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
cout << "Enter the elements of the array (sorted). Press -1 to stop. Max 10 elements. " << endl; for(int i = 0; i < 10; i++) { int temp = 0; cin >> temp; if(temp != -1) { array[i] = temp; ptr += 1; ctr++; } else break; } int elem = 0, location = -1; cout << "Which element must be inserted?" << endl; cin >> elem; cout << "At which place must it be inserted? (Indexing starts at 0)" << endl; cin >> location; int finalIndex = (ptr-array)/sizeof(int); for(int i = ctr; i >= location;i--) { array[i+1] = array[i]; } array[location] = elem; for(int i = 0;i <= ctr; i++) cout << array[i];
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
system("pause"); return 0; }
4.
/* By Akshay Tiwary Date: 30/03/13 This program uses pointers to add two matrices and returns the pointer to the resultant matrix. */ #include<iostream> using namespace std; int result[2][2]; int* add(int a[2][2],int b[2][2]) { int* ptr = &result[0][0]; for(int i = 0; i < 2;i++) for(int j = 0; j < 2;j++) *(*(result + i) + j) = *(*(a + i) + j) + *(*(b + i) + j); return ptr; } int main() { int a[2][2] = {{1,2},{3,4}}; int b[2][2] = {{5,6},{7,8}}; int *ptr = add(a,b); for (int i = 0;i < 4;i++) { cout << *(ptr + i) << " "; if(i % 2 == 1)
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
5.
/* By Akshay Tiwary Date: 30/03/13 This program transposes the matrix passed as parameter and returns a pointer to the transposed matrix. */ #include<iostream> using namespace std; int result[3][3]; int* transpose(int a[3][3]); int main() { int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int *ptr = transpose(a); for(int i = 0; i < 9;i++) { cout << *(ptr+i) << " "; if(i % 3 == 2) cout << endl; } system("pause"); return 0; }
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
int* transpose(int a[3][3]) { int* ptr = &result[0][0]; for(int i = 0; i < 3;i++) for(int j = 0; j < 3;j++) result[j][i] = a[i][j]; return ptr; }
6.
/* By Akshay Tiwary Date: 30/03/13 This program deletes all occurrences of a given character in a specified string. */ #include<iostream> using namespace std; char deletedString[20]; char* deleteOccurances(char* string, char value) { int ctr = 0; int i = 0; while(string[i] != '\0') { if(string[i] != value) { deletedString[ctr] = string[i]; ctr++; } i++; } return deletedString; }
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
int main() { char string[20] = "Hello World"; char value = 'l'; char* result = deleteOccurances(string,value); cout << "The modified string is " << result << endl; system("pause"); return 0; }
7.
/* By Akshay Tiwary Date: 30/03/13 This program creates a menu to perform string functions using pointers. */ #include<iostream> using namespace std; char result[20]; char* strCpy(char *s1, char* s2) { int ctr = 0; while(*(s2 + ctr) != '\0') { *(s1 + ctr) = *(s2 + ctr); ctr++; } *(s1 + ctr) = '\0'; return s1; }
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
int strLen(char *s1) { int ctr = 0; while(*(s1 + ctr) != '\0') { ctr++; } return ctr; } char* strRev(char *s1) { int len = strLen(s1); for(int i = len-1,j = 0; i >= 0;i--,j++) result[j] = *(s1 + i); return result; } char* strAppend(char *s1, char* s2) { int l1 = strLen(s1); int l2 = strLen(s2); for(int i = 0; i < l1;i++) *(result + i) = *(s1 + i); for(int i = l1,j = 0; i < l2;i++,j++) *(result + i) = *(s2 + j); return result; } int strCmp(char* s1, char* s2) { int ctr = 0; char* ptr1 = s1, *ptr2 = s2;
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
int main() { int ch = 10, len = 0, compare = 0; char a[20],*b, temp[20]; while (ch != 6) { cout << "Welcome to the Menu." << endl; cout << " 1. strCpy() \n 2. strRev() \n 3. strLen() \n 4. strAppend() \n 5. strCmp() \n 6.Exit() " << endl; cin >> ch; switch(ch) { case 1: cout << "Enter the string to be copied." << endl; cin >> a; b = strCpy(b,a); for(int i = 0; i < strLen(a); i++) cout << *(b + i); cout << endl; break; case 2: cout << "Enter the string to be reversed." << endl;
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
cin >> a; b = strRev(a); for(int i = 0; i < strLen(a); i++) cout << *(b + i); cout << endl; break; case 3: cout << "Enter the string whose length is sought." << endl; cin >> a; len = strLen(a); cout << "The length of the string is " << len << endl; break; case 4: cout << "Enter the first string." << endl; cin >> a; cout << "Enter the second string." << endl; cin >> temp; b = strAppend(a,temp); for(int i = 0; i < (strLen(a)+strLen(temp)-1); i++) cout << *(b + i); cout << endl; break; case 5: cout << "Enter the first string." << endl; cin >> a;
Sri Kumarans Childrens Home Akshay Tiwary XII A
Assignment 1
Pointers
cout << "Enter the second string." << endl; cin >> temp; compare = strCmp(a,temp); cout << "The value after compare was " << compare << endl; break; case 6: cout << "The system will now exit." << endl; break; } } system("pause"); return 0; }
8.
/* By Akshay Tiwary Date: 30/03/13 This program creates a 2 D array using pointers which stores 5 strings. Sort the array using pointers. */ #include<iostream> using namespace std; void swap(const char*& c, const char*& d) { const char* temp = c; c = d; d = temp; }
Assignment 1
Pointers
{ for (int i = 0; i < 3; i++) { int nSmallestIndex = i; for (int j = i + 1; j < 3; j++) { if (strcmp(str[j],str[nSmallestIndex]) < 0) nSmallestIndex = j; } swap(str[i], str[nSmallestIndex]); } return str[0]; } int main() { char names[3][10] = {"Akshay","Rohit","Ashwin"}; char* ptr = sort(names); for(int i = 0; i < 3;i++) { cout << names[i]; } cout << endl; system("pause"); return 0; }