In this program, a structure, student is created. This structure has three members: name (string), roll (integer) and marks (float). Then, we created a structure array of size 10 to store information of 10 students. Using for loop, the program takes the information of 10 students from the user and displays it on the screen.
Enter information of students: For roll number1,Enter name: TomEnter marks: 98For roll number2,Enter name: JerryEnter marks: 89...Displaying Information:Roll number: 1Name: TomMarks: 98...
Calculate Difference Between Two Time Period
// Computes time difference of two time period// Time periods are entered by the user#include<iostream>usingnamespace std;structTIME{int seconds;int minutes;int hours;};voidcomputeTimeDifference(struct TIME,struct TIME,structTIME*);intmain(){structTIMEt1, t2, difference; cout <<"Enter start time."<< endl; cout <<"Enter hours, minutes and seconds respectively: "; cin >>t1.hours >>t1.minutes >>t1.seconds; cout <<"Enter stop time."<< endl; cout <<"Enter hours, minutes and seconds respectively: "; cin >>t2.hours >>t2.minutes >>t2.seconds;computeTimeDifference(t1, t2,&difference); cout << endl <<"TIME DIFFERENCE: "<<t1.hours <<":"<<t1.minutes <<":"<<t1.seconds; cout <<" - "<<t2.hours <<":"<<t2.minutes <<":"<<t2.seconds; cout <<" = "<<difference.hours <<":"<<difference.minutes <<":"<<difference.seconds;return0;}voidcomputeTimeDifference(structTIME t1,structTIME t2,structTIME*difference){if(t2.seconds >t1.seconds) {--t1.minutes;t1.seconds +=60; }difference->seconds =t1.seconds -t2.seconds;if(t2.minutes >t1.minutes) {--t1.hours;t1.minutes +=60; }difference->minutes =t1.minutes-t2.minutes;difference->hours =t1.hours-t2.hours;}
Output
Enter hours, minutes and seconds respectively: 113352Enter stop time.Enter hours, minutes and seconds respectively: 81215TIME DIFFERENCE: 11:33:52-8:12:15=3:21:37
In this program, user is asked to enter two time periods and these two periods are stored in structure variables t1 and t2 respectively.
Then, the computeTimeDifference() function calculates the difference between the time periods and the result is displayed on the screen from the main() function without returning it (call by reference).
Add Complex Numbers by Passing Structure to a Function
// Complex numbers are entered by the user#include<iostream>usingnamespace std;typedefstructcomplex{float real;float imag;} complexNumber;complexNumberaddComplexNumbers(complex,complex);intmain(){ complexNumber n1, n2, temporaryNumber;char signOfImag; cout <<"For 1st complex number,"<< endl; cout <<"Enter real and imaginary parts respectively:"<< endl; cin >>n1.real >>n1.imag; cout << endl <<"For 2nd complex number,"<< endl; cout <<"Enter real and imaginary parts respectively:"<< endl; cin >>n2.real >>n2.imag; signOfImag = (temporaryNumber.imag >0) ?'+':'-';temporaryNumber.imag = (temporaryNumber.imag >0) ?temporaryNumber.imag :-temporaryNumber.imag; temporaryNumber =addComplexNumbers(n1, n2); cout <<"Sum = "<<temporaryNumber.real <<temporaryNumber.imag <<"i";return0;}complexNumberaddComplexNumbers(complex n1,complex n2){ complex temp;temp.real =n1.real+n2.real;temp.imag =n1.imag+n2.imag;return(temp);}
Output
Enter real and imaginary parts respectively:3.45.5For 2nd complex number,Enter real and imaginary parts respectively:-4.5-9.5Sum =-1.1-4i
In the problem, two complex numbers entered by the user is stored in structures n1 and n2. These two structures are passed to addComplexNumbers() function which calculates the sum and returns the result to the main() function. Finally, the sum is displayed from the main() function.
Add Two Distances (in inch-feet) System Using Structures
#include<iostream>usingnamespace std;structDistance{int feet;float inch;}d1 , d2, sum;intmain(){ cout <<"Enter 1st distance,"<< endl; cout <<"Enter feet: "; cin >>d1.feet; cout <<"Enter inch: "; cin >>d1.inch; cout <<"\nEnter information for 2nd distance"<< endl; cout <<"Enter feet: "; cin >>d2.feet; cout <<"Enter inch: "; cin >>d2.inch;sum.feet =d1.feet+d2.feet;sum.inch =d1.inch+d2.inch; // changing to feet if inch is greater than 12if(sum.inch >12) {++sum.feet;sum.inch -=12; } cout << endl <<"Sum of distances = "<<sum.feet <<" feet "<<sum.inch <<" inches";return0;}
Output
Enter 1st distance,Enter feet: 6Enter inch: 3.4Enter information for2nd distanceEnter feet: 5Enter inch: 10.2Sum of distances =12 feet 1.6 inches
In this program, a structure Distance containing two data members (inch and feet) is declared to store the distance in inch-feet system. Here, two structure variables d1 and d2 are created to store the distance entered by the user. And, the sum variables stores the sum of the distances. The if..else statement is used to convert inches to feet if the value of inch of sum variable is greater than 12.
Store Information of a Student in a Structure
In this program, a structure(student) is created which contains name, roll and marks as its data member. Then, a structure variable(s) is created. Then, data (name, roll and marks) is taken from user and stored in data members of structure variable s. Finally, the data entered by user is displayed.
Enter information,Enter name: BillEnter roll number: 4Enter marks: 55.6Displaying Information,Name: BillRoll: 4Marks: 55.6
In this program, student (structure) is created. This structure has three members: name (string), roll (integer) and marks (float). Then, a structure variable s is created to store information and display it on the screen.
Sort Elements in Lexicographical Order (Dictionary Order)
This program takes 10 words from the user and sort them in lexicographical order.
#include<iostream>usingnamespace std;intmain(){ string str[10], temp; cout <<"Enter 10 words: "<< endl;for(int i =0; i <10; ++i) {getline(cin,str[i]); }for(int i =0; i <9; ++i)for( int j = i+1; j <10; ++j) {if(str[i] >str[j]) { temp =str[i];str[i] =str[j];str[j] = temp; } } cout <<"In lexicographical order: "<< endl;for(int i =0; i <10; ++i) { cout <<str[i] << endl; }return0;}
Output
Enter 10 words: C C++JavaPythonPerlRMatlabRubyJavaScriptPHPIn lexicographical order: CC++JavaJavaScriptMatlabPHPPerlPythonRRuby
To solve this program, an array of string object str[10] is created. The 10 words entered by the user is stored in this array. Then, the array is sorted in lexicographical order using nested for loop and displayed on the screen.
Enter string s1: I love Enter string s2: C++ programming s1 = I love C++ programmings2 = C++ programming
Find the Length of a String
You can get the length of a string object by using a size() function or a length() function.
The size() and length() functions are just synonyms and they both do exactly same thing.
Length of String Object
#include<iostream>usingnamespace std;intmain(){ string str ="C++ Programming"; // you can also use str.length() cout <<"String Length = "<<str.size();return0;}
Output
String Length =15
Length of C-style string
To get the length of a C-string string, strlen() function is used.
#include<iostream>#include<cstring>usingnamespace std;intmain(){char str[] ="C++ Programming is awesome"; // you can also use str.length() cout <<"String Length = "<<strlen(str);return0;}
Output
String Length = 26
Remove all Characters in a String Except Alphabets
Remove all characters except alphabets
This program takes a string (object) input from the user and removes all characters except alphabets.
Enter a line of string: I have 2 C++ programming books.Vowels: 8Consonants: 14Digits: 1White spaces: 5
Find the Frequency of Characters in a String
In this example, frequency of characters in a string object is computed. To do this, size() function is used to find the length of a string object. Then, the for loop is iterated until the end of the string. In each iteration, occurrence of character is checked and if found, the value of count is incremented by 1.
Find Frequency of Characters of a String Object
#include<iostream>usingnamespace std;intmain(){ string str ="C++ Programming is awesome";char checkCharacter ='a';int count =0;for (int i =0; i <str.size(); i++) {if (str[i] == checkCharacter) {++ count; } } cout <<"Number of "<< checkCharacter <<" = "<< count;return0;}
Output
Number of a =2
In the example below, loop is iterated until the null character '\0' is encountered. Null character indicates the end of the string.
In each iteration, the occurrence of the character is checked.
Find Frequency of Characters in a C-style String
#include<iostream>usingnamespace std;intmain(){char c[] ="C++ programming is not easy.", check ='m';int count =0;for(int i =0; c[i] !='\0'; ++i) {if(check ==c[i])++count; } cout <<"Frequency of "<< check <<" = "<< count;return0;}
Output
Number of m =2
Swap Numbers in Cyclic Order Using Call by Reference
Three variables entered by the user are stored in variables a, b and c respectively.
Then, these variables are passed to the function cyclicSwap(). Instead of passing the actual variables, addresses of these variables are passed.
When these variables are swapped in cyclic order in the cyclicSwap() function, variables a, band c in the main function are also automatically swapped.
Program to Swap Elements Using Call by Reference
#include<iostream>usingnamespace std;voidcyclicSwap(int*a,int*b,int*c);intmain(){int a, b, c; cout <<"Enter value of a, b and c respectively: "; cin >> a >> b >> c; cout <<"Value before swapping: "<< endl; cout <<"a, b and c respectively are: "<< a <<", "<< b <<", "<< c << endl;cyclicSwap(&a,&b,&c); cout <<"Value after swapping numbers in cycle: "<< endl; cout <<"a, b and c respectively are: "<< a <<", "<< b <<", "<< c << endl;return0;}voidcyclicSwap(int*a,int*b,int*c){int temp; temp =*b;*b =*a;*a =*c;*c = temp;}
Output
Enter value of a, b and c respectively: 123Value before swapping: a=1b=2c=3Value after swapping numbers in cycle:a=3b=1c=2
Notice that we haven't returned any values from the cyclicSwap() function.
Access Elements of an Array Using Pointer
#include<iostream>usingnamespace std;intmain(){intdata[5]; cout <<"Enter elements: ";for(int i =0; i <5; ++i) cin >>data[i]; cout <<"You entered: ";for(int i =0; i <5; ++i) cout << endl <<*(data + i);return0;}
Output
Enter elements: 12354You entered: 12354
In this program, the five elements are entered by the user and stored in the integer array data. Then, the data array is accessed using a for loop and each element in the array is printed onto the screen. Visit this page to learn about relationship between pointer and arrays.