Tricky Questions and Programs on C++
Q: What do you mean my data types? what are the types of data types in C++ & their value ranges? Show Ans
A: Data Type defines the value which any variable is going to store. Data Types are of two types Pre-defined(Premitive) and User-Defined (Non Premitive)
Following table lists down seven basic C++ data types −
Premitive (Pre defined) | Non Primitive (User defined) |
---|---|
bool | class |
char | structure |
int | enum |
float | union |
double | |
void | |
wchar_t |
few basic types can be also be modified using one or more of these type modifiers −
- signed
- unsigned
- short
- long
The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.
Please note - Size of data types depends on Compiler. These figures are on 32 bit compiler
Type | Typical Bit Width | Typical Range |
---|---|---|
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 4bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 4bytes | same as long int |
unsigned long int | 4bytes | 0 to 4,294,967,295 |
float | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
Q: C++ program to find area using constants and defines. Show Ans
#include #define length 15 #define width 20 const int len = 15; const int wid = 20; using namespace std; int main() { cout<<"\n Area using #define : " << length * width; cout<<"\n Area using constants : "<< len * wid; return 0; }
Q: C++ program to sort integer array in asscending order. Show Ans
#include using namespace std; int main() { int ARR[10] = {4,3,8,0,12,4,14,43,10,9}; for(int i = 0; i < 10 -1; i++) { for(int j= i + 1 ; j < 10 ;j++) { if(ARR[j] > ARR[i]) { int temp = ARR[i]; ARR[i] = ARR[j]; ARR[j] = temp; } } } cout<<"\n Result after sorting array in assending order:- \n "; for(int i = 0; i< 10; i++) { cout<<" " << ARR[i]; } return 0; }
Q: What is the difference between goto & loops? Show Ans
while, do-while() and for loops get compiled to the same thing as goto, so it usually won't make a difference. If you have your doubts, you can feel free to try all three and see which takes longer. You'll be unable to measure a difference, even if you loop a billion times.
the goto loop
int i=3; loop: printf("\n goto vs loops"); if(--i) goto loop;
the while loop
int i=3; while(i--) { printf("\n goto vs loops"); }
the for loop
for(int i=3; i; i--) { printf("\n goto vs loops"); }
do while loop
int i =3; do { printf("\n goto vs loops"); i--; }while(i>0);
Now I will show you one interesting things which you can do in loops but not with goto:-
for(int i=0; i
And there are few minor differences:-
- If identation is not correct then code is not readable with multiple goto in our code
- In for loop it is easy to understand the conditions but in case of goto we have to look up & down to see the condition
- programmers never prefers use of goto as compare to loops
NOTE − Use of goto statement is highly discouraged bye the programmers because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto.
Q: Write C++ Program to read student data into array of strucutre from a text file. Show Ans
#include #include #include #include #include #include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct student { string stnumber; string stname; string gender; double quizz1; double quizz2; double assignment; double midterm; double finalmarks; double total; double avg; }students; students obj[10]; void readRecordsintoStruture() { ifstream in("file.txt"); if(!in) { cout << "Cannot open input file - may be file not there in correct directory\n"; } char str[500]; int row = 0; while(in && row <10) { in.getline(str, 500); // delim defaults to '\n' //if(in) cout << str << endl; //char *ptr = strtok_s (str," ,"); char* context = NULL; char *ptr = strtok_s(str, ",", &context); if(ptr!= NULL) { //read stnumber from file obj[row].stnumber = std::string(ptr); cout<< obj[row].stnumber <<", " ; ptr = strtok_s (NULL, ",", &context); //read stname form file obj[row].stname= std::string(ptr); cout<< obj[row].stname <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read gender form file obj[row].gender = std::string(ptr);cout<< obj[row].gender <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read quizz1 form file obj[row].quizz1 = atof(ptr);cout<< obj[row].quizz1 <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read quizz2 form file obj[row].quizz2 = atof(ptr);cout<< obj[row].quizz2 <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read assignment form file obj[row].assignment = atof(ptr);cout<< obj[row].assignment <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read midterm form file obj[row].midterm = atof(ptr);cout<< obj[row].midterm <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read finalmarks form file obj[row].finalmarks = atof(ptr);cout<< obj[row].finalmarks <<", "; //ptr = strtok_s (NULL, ","); ptr = strtok_s (NULL, ",", &context); //read total form file obj[row].total = atof(ptr);cout<< obj[row].total <<", \n"; } row++; } in.close(); } void printMaxMarkStudent() { float Totalmarks = 0; for(int i = 0; i<10; i++) { if(obj[i].total > Totalmarks) Totalmarks = obj[i].total; } cout<<"\n Maximum marks : " << Totalmarks; } void deleteRecord(string stnumber) { bool check = false; for(int i = 0; i<9; i++) { if(obj[i].stnumber.compare(stnumber) == 0 || (check==true)) { check=true; obj[i].stnumber = obj[i+1].stnumber; obj[i].stname = obj[i+1].stname; obj[i].gender = obj[i+1].gender; obj[i].quizz1 = obj[i+1].quizz1; obj[i].quizz2 = obj[i+1].quizz2; obj[i].assignment = obj[i+1].assignment; obj[i].midterm = obj[i+1].midterm; obj[i].finalmarks = obj[i+1].finalmarks; obj[i].total = obj[i+1].total; } } } void calcualteStdAvg(string stnumber) { for(int row = 0; row<10; row++) { float avg = 0; avg = obj[row].total; avg = avg/5; cout<<"\n Avg of -"<
//content of file.txt - create this file in current diretory of .cpp file
/*
101,Francisco,Male,78,67,70,55,59,329
102,migle kunigonyte,Male,77,66,69,56,55,323
103,elena pusciute, female ,76, 67, 65, 66, 59, 333
104, terese pusciute, female, 65, 65, 55, 56, 54, 295
105, monika silkaityte, female, 73, 65, 59, 69, 68, 334
106, rokas sinkevicius, male, 63, 65, 59, 69, 68, 324
107, nurulas sabidas, male, 63, 75, 59, 79, 68, 344
108, mindaugas puskorius, male, 63, 65, 59, 69, 68, 324
109, mac mishra, male, 63, 65, 59, 59, 68, 314
110, shehram ali, male, 63, 65, 59, 59, 78, 324
*/
Problems
Projects
Compiler
About Us | Contact Us | Privacy Policy | Career | Online Training
Linkedin Facebook Twitter Instagram Youtube
Copyright©CppBuzz.com
Like many websites, we use cookies to ensure best browsing experience on our website. While using this website, you acknowledge to have read and accepted our cookie and privacy policy.