Skip to content

Commit de03f45

Browse files
committed
files added
0 parents  commit de03f45

File tree

18 files changed

+883
-0
lines changed

18 files changed

+883
-0
lines changed

Beginners_1/Stage_1/manual.docx

27.8 KB
Binary file not shown.

Beginners_1/Stage_1/task1.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
- Declare two integer variables 'a' and 'b'.
3+
- Create two integer pointers 'ptrA' and 'ptrB' and assign them the addresses of 'a' and 'b' respectively.
4+
- Prompt the user to input two integer values, which are stored in 'a' and 'b'.
5+
- Display the values of 'a' and 'b' using the pointers 'ptrA' and 'ptrB' respectively.
6+
- Update the values of 'a' and 'b' by setting the values at the memory addresses pointed to by 'ptrA' and 'ptrB' to 30 and 40 respectively.
7+
- Display the updated values of 'a' and 'b' using the pointers 'ptrA' and 'ptrB' respectively.
8+
*/
9+
#include <iostream>
10+
using namespace std;
11+
int main(){
12+
int a,b;
13+
int *ptrA=&a;
14+
int *ptrB=&b;
15+
16+
cout<<"Enter first number = ";
17+
cin>>a;
18+
19+
cout<<"Enter second number = ";
20+
cin>>b;
21+
22+
cout<<"the values are = "<<*ptrA<<" & "<<*ptrB<<endl;
23+
24+
*ptrA=30;
25+
*ptrB=40;
26+
27+
cout<<"Updated values of *ptrA and *ptrB are = "<<*ptrA<<" & "<<*ptrB;
28+
29+
return 0;
30+
}

Beginners_1/Stage_1/task2.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
This C++ program demonstrates the use of dynamic memory allocation and pointer operations to evaluate
3+
the square, cube, and half of a user-provided number.
4+
5+
- Declare an integer pointer 'ptr' to hold a memory address for an integer value.
6+
- Dynamically allocate memory for an integer using the 'new' operator, and store the address in 'ptr'.
7+
- Prompt the user to enter a number, which is stored in the memory location pointed to by 'ptr'.
8+
- Display the square of the entered number by multiplying the value at 'ptr' with itself.
9+
- Display the cube of the entered number by multiplying the value at 'ptr' with itself twice.
10+
- Display half of the entered number by dividing the value at 'ptr' by 2.
11+
- Deallocate the memory previously allocated for the integer using the 'delete' operator to avoid memory leaks.
12+
13+
Note: Dynamic memory allocation using 'new' allows for creating variables at runtime and is useful when the
14+
size of memory needed is not known at compile time. However, it is crucial to deallocate the memory using 'delete'
15+
to avoid memory leaks and free up memory for reuse.
16+
*/
17+
#include <iostream>
18+
using namespace std;
19+
int main(){
20+
int *ptr;
21+
ptr = new int;
22+
23+
cout<<"Enter a number for the evaluation = ";
24+
cin>>*ptr;
25+
26+
cout<<"Square of a number is = ";
27+
cout<<(*ptr)*(*ptr)<<endl;
28+
29+
cout<<"Cube of a number is = ";
30+
cout<<(*ptr)*(*ptr)*(*ptr)<<endl;
31+
32+
cout<<"Half of a number is = ";
33+
cout<<(*ptr)/2<<endl;
34+
35+
delete ptr;
36+
37+
return 0;
38+
}

Beginners_1/Stage_1/task3.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This C++ program demonstrates the use of dynamic memory allocation and pointer operations to evaluate
3+
the square, cube, and half of a user-provided number.
4+
5+
- Declare an integer pointer 'ptr' to hold a memory address for an integer value.
6+
- Dynamically allocate memory for an integer using the 'new' operator, and store the address in 'ptr'.
7+
- Prompt the user to enter a number, which is stored in the memory location pointed to by 'ptr'.
8+
- Display the square of the entered number by multiplying the value at 'ptr' with itself.
9+
- Display the cube of the entered number by multiplying the value at 'ptr' with itself twice.
10+
- Display half of the entered number by dividing the value at 'ptr' by 2.
11+
- Deallocate the memory previously allocated for the integer using the 'delete' operator to avoid memory leaks.
12+
13+
Note: Dynamic memory allocation using 'new' allows for creating variables at runtime and is useful when the
14+
size of memory needed is not known at compile time. However, it is crucial to deallocate the memory using 'delete'
15+
to avoid memory leaks and free up memory for reuse.
16+
*/
17+
/*
18+
This C++ program allows the user to input the size of an array and then takes input for the array elements.
19+
It then finds and displays the index of the second largest value in the array.
20+
21+
- Prompt the user to enter the limit (size) of the array.
22+
- Create a dynamically allocated integer array 'arr' of size 'limit' to store the elements.
23+
- Prompt the user to input 'limit' number of integer values, which are stored in the array 'arr'.
24+
- Initialize variables 'largest' and 'secondmax' to keep track of the index of the largest and second largest values in the array.
25+
- Iterate through the array elements and find the index of the largest value, storing it in 'largest'.
26+
- While finding the largest value, also update 'secondmax' to keep track of the index of the second largest value.
27+
- Display the index of the second largest value found in the array.
28+
29+
Note: The program uses dynamic memory allocation to create an array with a size specified by the user. By iterating
30+
through the array, it identifies the index of the second largest value without sorting the array explicitly.
31+
*/
32+
#include <iostream>
33+
using namespace std;
34+
int main(){
35+
36+
int limit;
37+
cout<<"Enter limit of the array = ";
38+
cin>>limit;
39+
40+
int *arr=new int [limit];
41+
cout<<"Enter values upto "<<limit<<endl;
42+
43+
for(int i=0;i<limit;i++){
44+
cin>>arr[i];
45+
}
46+
int largest=0,secondmax=0;
47+
48+
for (int i=0;i<limit;i++){
49+
if(arr[i]>arr[largest]){
50+
secondmax=largest;
51+
largest=i;
52+
}
53+
}
54+
cout<<endl;
55+
cout<<"Index of the 2nd largest value is = ";
56+
cout<<secondmax;
57+
58+
return 0;
59+
60+
61+
}

Beginners_1/Stage_1/task4.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This C++ program allows the user to input the size of an array and then takes input for the array elements.
3+
It then adds 3 to each element of the array and displays the updated array.
4+
5+
- Prompt the user to enter the limit (size) of the array.
6+
- Check if the entered limit is non-positive (less than or equal to 0), and if so, display an error message and terminate the program.
7+
- Create a dynamically allocated integer array 'arr' of size 'limit' to store the elements.
8+
- Prompt the user to input 'limit' number of integer values, which are stored in the array 'arr'.
9+
- Create an integer pointer 'ptr' and set it to point to the first element of the 'arr' array.
10+
- Iterate through the array 'arr' using the pointer 'ptr', and for each element, add 3 to its value using the pointer.
11+
- Display the updated array 'arr' to show the values after adding 3 to each element.
12+
13+
Note: The program uses dynamic memory allocation to create an array with a size specified by the user. It then manipulates
14+
the array elements through a pointer 'ptr', adding 3 to each element directly via pointer manipulation.
15+
*/
16+
#include <iostream>
17+
using namespace std;
18+
int main(){
19+
int limit;
20+
cout<<"Enter limit of the array = ";
21+
cin>>limit;
22+
23+
if(limit<=0){
24+
cout<<"Enter valid and positve limit !!!";
25+
}
26+
27+
int *arr=new int [limit];
28+
cout<<"Enter values upto "<<limit<<endl;
29+
30+
for(int i=0;i<limit;i++){
31+
cin>>arr[i];
32+
}
33+
34+
int *ptr;
35+
ptr=arr;
36+
37+
for(int i=0;i<limit;i++){
38+
*ptr+=3;
39+
ptr++;
40+
}
41+
cout<<endl;
42+
cout<<"The values after adding 3 are = ";
43+
44+
for(int i=0;i<limit;i++){
45+
cout<<arr[i];
46+
cout<<" ";
47+
}
48+
49+
return 0;
50+
}

Beginners_1/Stage_1/task5.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
This C++ program calculates the median of three constant integer values 'a', 'b', and 'c'.
3+
It uses pointers to compare the values and finds the median by checking the order of the values.
4+
5+
- Declare three constant integer variables 'a', 'b', and 'c' with initial values.
6+
- Declare three constant integer pointers 'pa', 'pb', and 'pc' and assign them the addresses of 'a', 'b', and 'c' respectively.
7+
- Declare an integer variable 'median' to store the calculated median value.
8+
- Compare the three values using pointers to determine the median:
9+
- If 'a' is in between 'b' and 'c' or vice versa, set 'median' to 'a'.
10+
- If 'b' is in between 'a' and 'c' or vice versa, set 'median' to 'b'.
11+
- Otherwise, set 'median' to 'c'.
12+
- The program then uses const_cast to modify the constant values of 'a', 'b', or 'c' to -1 through the pointers,
13+
effectively updating the value that was used as the median.
14+
- Display the calculated median value.
15+
- Display the updated values of 'a', 'b', and 'c' to show that one of them is now -1.
16+
17+
Note: The const_cast is used to modify the constant values, but it is generally not recommended to modify
18+
constant variables as it can lead to undefined behavior. This code is for illustrative purposes only.
19+
*/
20+
#include <iostream>
21+
using namespace std;
22+
23+
int main() {
24+
const int a = 5;
25+
const int b = 12;
26+
const int c = 10;
27+
const int *pa = &a;
28+
const int *pb = &b;
29+
const int *pc = &c;
30+
31+
int median = 0;
32+
33+
if ((*pa <= *pb && *pa >= *pc) || (*pa >= *pb && *pa <= *pc)) {
34+
median = *pa;
35+
const_cast<int&>(a) = -1; // Updating median value to -1 through pointers
36+
} else if ((*pb <= *pa && *pb >= *pc) || (*pb >= *pa && *pb <= *pc)) {
37+
median = *pb;
38+
const_cast<int&>(b) = -1; // Updating median value to -1 through pointers
39+
} else {
40+
median = *pc;
41+
const_cast<int&>(c) = -1; // Updating median value to -1 through pointers
42+
}
43+
44+
cout << "Median is = " << median << endl;
45+
cout << "Updated values of a, b, and c are: " << a << ", " << b << ", " << c << endl;
46+
47+
return 0;
48+
}

Beginners_1/Stage_2/manual.docx

26.2 KB
Binary file not shown.

Beginners_1/Stage_2/task1.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This C++ program demonstrates the dynamic input, compression, and output of an integer array.
3+
4+
- The function 'InputArray' takes a reference to an integer 'size' and dynamically allocates memory for an integer array.
5+
It prompts the user to input the size and elements of the array and returns a pointer to the dynamically created array.
6+
7+
- The function 'OutputArray' takes a constant integer pointer 'myarray' and a constant integer reference 'size'.
8+
It displays the elements of the array pointed to by 'myarray' using pointer arithmetic.
9+
10+
- The function 'CompressArray' takes an integer pointer 'originalArr' and a reference to an integer 'size'.
11+
It creates a new dynamically allocated integer array 'compressedArr' to store the compressed version of the original array.
12+
The function compresses the original array by removing duplicate consecutive elements.
13+
It then updates the 'size' parameter with the new compressed size, deallocates the memory of the original array, and returns the compressed array.
14+
15+
- In the 'main' function, the user is prompted to input the length of the array.
16+
It calls the 'InputArray' function to dynamically create the array and fill it with user-provided values.
17+
It then displays the original array using the 'OutputArray' function.
18+
19+
- The 'CompressArray' function is called to obtain the compressed version of the original array.
20+
The compressed array is then displayed using the 'OutputArray' function.
21+
22+
- Finally, memory allocated for the compressed array is deallocated using 'delete[]' to prevent memory leaks.
23+
24+
Note: The program showcases dynamic memory allocation and manipulation of arrays using pointers.
25+
Remember to free up the memory allocated using 'new' with 'delete[]' to prevent memory leaks.
26+
*/
27+
#include <iostream>
28+
using namespace std;
29+
30+
// Function to input an array dynamically
31+
int* InputArray(int& size) {
32+
cout << "Enter size of the array: ";
33+
cin >> size;
34+
35+
int* array = new int[size];
36+
37+
cout << "Enter " << size << " elements: ";
38+
for (int i = 0; i < size; i++) {
39+
cin >> array[i];
40+
}
41+
42+
return array;
43+
}
44+
45+
// Function to output an array
46+
void OutputArray(const int* myarray, const int& size) {
47+
for (int i = 0; i < size; i++) {
48+
cout << *(myarray + i) << " ";
49+
}
50+
}
51+
52+
// Function to compress an array
53+
int* CompressArray(int* originalArr, int& size) {
54+
int* compressedArr = new int[size];
55+
int compressedSize = 0;
56+
57+
compressedArr[compressedSize++] = originalArr[0];
58+
for (int i = 1; i < size; i++) {
59+
if (originalArr[i] != originalArr[i - 1]) {
60+
compressedArr[compressedSize++] = originalArr[i];
61+
}
62+
}
63+
64+
// Update the size parameter with the compressed size
65+
size = compressedSize;
66+
67+
// Delete the original array to prevent memory leaks
68+
delete[] originalArr;
69+
70+
return compressedArr;
71+
}
72+
73+
int main() {
74+
int length;
75+
cout << "Enter length of the array: ";
76+
cin >> length;
77+
78+
int* array = InputArray(length);
79+
80+
cout << "The values are: ";
81+
OutputArray(array, length);
82+
cout << endl;
83+
84+
int compressedSize = length;
85+
int* compressedArray = CompressArray(array, compressedSize);
86+
87+
cout << "Array after Compression: ";
88+
OutputArray(compressedArray, compressedSize);
89+
cout << endl;
90+
91+
// Don't forget to deallocate the memory
92+
delete[] compressedArray;
93+
94+
return 0;
95+
}

Beginners_1/Stage_2/task2.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
This C++ program takes an input string, counts and categorizes various elements in the string.
3+
4+
- The 'Counter' function takes a character array 'array' as input and counts the following elements:
5+
- 'spaces': Counts the number of space characters in the input array.
6+
- 'uppercase': Counts the number of uppercase letters in the input array.
7+
- 'lowercase': Counts the number of lowercase letters in the input array.
8+
- 'vowels': Counts the number of vowels (a, e, i, o, u, both uppercase and lowercase) in the input array.
9+
- 'consonant': Calculates the number of consonants (total letters - vowels) in the input array.
10+
11+
- In the 'main' function, the user is prompted to enter an array of characters (string) using 'cin.getline()'.
12+
The 'Counter' function is then called with this array as an argument to analyze the content of the input string.
13+
14+
- The 'Counter' function prints the total number of letters (uppercase and lowercase combined), spaces,
15+
uppercase letters, lowercase letters, vowels, and consonants in the input string.
16+
17+
Note: The program showcases basic string manipulation using character arrays and various character count calculations.
18+
The 'Counter' function iterates through the input array and counts the desired elements based on specific conditions.
19+
The use of 'cin.getline()' allows the program to read spaces and accept input of multiple words in a single line.
20+
*/
21+
#include<iostream>
22+
#include <stdio.h>
23+
#include <cstring>
24+
#include <string>
25+
using namespace std;
26+
void Counter(char array[]){
27+
int spaces=0;
28+
int uppercase=0;
29+
int lowercase=0;
30+
int vowels=0;
31+
int cosonant=0;
32+
int i=0;
33+
for(int i=0;i<strlen(array);i++){
34+
if(array[i]==32){
35+
spaces++;
36+
}
37+
if(array[i]>='A' && array[i]<='Z'){
38+
uppercase++;
39+
// cout<<uppercase;
40+
}
41+
else if(array[i]>='a' && array[i]<='z'){
42+
lowercase++;
43+
//cout<<lowercase;
44+
}
45+
if(array[i]=='a'||array[i]=='e'||array[i]=='i'||array[i]=='o'||array[i]=='u'){
46+
vowels++;
47+
//cout<<vowels;
48+
}
49+
}
50+
cout<<"Total lettes are :"<<uppercase+lowercase<<endl;
51+
cout<<"Total spaces are :"<<spaces<<endl;
52+
cout<<"Upper case letters are :"<<uppercase<<endl;
53+
cout<<"Lowercase letters are :"<<lowercase<<endl;
54+
cout<<"Vowels are :"<<vowels<<endl;
55+
cout<<"Consonants are :"<<(uppercase+lowercase)-vowels<<endl;
56+
57+
}
58+
59+
int main(){
60+
char array[50];
61+
cout<<"enter array"<<endl;
62+
cin.getline(array,50);
63+
Counter(array);
64+
system("pause");
65+
return 0;
66+
}
67+

0 commit comments

Comments
 (0)