"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Structure. Show all posts
Showing posts with label Structure. Show all posts

Tuesday, April 6, 2010

An array stores details of 25 students (rollno, name, marks in 3 subjects). Write a program to create such an array and print out a list of students who have failed in more than one subject. Assume 40% as pass marks.


#include<iostream.h>
#include<conio.h>

struct stud
{
int roll;
char nm[50];
float m1, m2, m3;
};

typedef stud S;

void main()
{
S student[25];

for(int i =0 ; i < 25 ; i++)
{
cout << "\n Enter Roll no :  ";
cin >> student[i].roll;

cout << "\n Enter Name : "
cin.getline(student[i].nm, 50);

cout << "\n Enter marks of three subjects :";
cin >> student[i].m1 >> student[i].m2 >> student[i].m3 ;

}

cout<< "\n STUDENTS FAILED IN MORE THAN 1 SUBJECT \n ";
for(i =0 ; i < 25 ; i++)
{
if(( student[i].m1< 40 && student[i].m2 < 40) || (student[i].m2 < 40 && student[i].m3 < 40) || ( student[i].m1 < 40 && student[i].m3 < 40))
cout << student[i].roll  << "\t" << student[i].nm << "\n";
}

getch();

}

A linear array if size 50 stores following informations : name of the country, country's capital and per capita income of the country. Write a complete program in C++ to do the following :

(a) To read a country's name and display capital and per-ca pita income.
(b) To read name of tha capital city and displays country's namr and displays country's name and per capital income.

Display an error message in case of an incorrect input.


#include<iostream.h>
#include<conio.h>
struct country
{
char nm[30];
char capital[30];
float income;
};

void main()
{
country c[50];
for( int i=0; i<50 ; i++)
{
cout << "\n Country's name : ";
cin.getline(c[i].nm, 30);
cout << "\n Country's capital :";
cin.getline(c[i].capital,30);
cout << "\n Per capita income :";
cin >> c[i].income;
}

clrscr();
cout << "\n\n Sl No. \t Country \t Capital \t Per-Ca pita-Income \n :";
for( int i=0; i < 50 ; i++)
{
cout << i+1 << "\t" << c[i].nm << "\t" << c[i].capital << "\t" << c[i].income << "\n" ;
}

cout << "\n *************************************************** \n ";

char ch = 'y';
char cap[30];
int flag = 0;

while(ch == 'y' || ch == 'Y')
{
cout << "\n Enter Capital name : ";
cin.getline(cap, 30);

for(int k =0; k <50 ; k++)
{
flag = 0;
if( strcmp( c[i].capital , cap ) == 0)
{
cout  << c[i].nm << "\t" << c[i].capital << "\t" << c[i].income << "\n" ;
flag = 1;
}

if(flag == 0)
cout << " Match not found !! ";
cout<< "Do you want to continue ?(y/n)";
cin >> ch;
}  
}
}

Friday, January 29, 2010

Write a program to prepare the invoice from the following data :

customer number
customer name and address
date of sale, description
quantity
unit price
discount percentage
sales tax percentage

===================================================


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

struct date{
int day;
int mon;
int yr;
};

struct invo{
int cust_num;
char cust_nm[20];
char add[40];
date D_O_sale;
char descp[30];
int quan;
int unit_pr;
float discount;
float sales_tax;
};

void main()
{
invo Customer[10];
char c;

for(int i = 0; i< 10; i++)
{
cout<< "\n Enter the customer no.";
cin>> Customer[i].cust_num;

cout<< "\n Enter the Customer name :";
gets( Customer[i].cust_nm);

cout<< "Enter Date of Sale (dd-mm-yy):";
cin >> Customer[i].D_O_Sale.day >> c >>  Customer[i].D_O_Sale.mon >>  c >> Customer[i].D_O_Sale.yr ;

cout<< "\n Enter Description :";
gets( Customer[i].descp);

cout<< "\nEnter Quantity : ";
cin>> Customer[i].quan;

cout<< "\n Unit Price ?";
cin >> Customer[i].unit_pr;

cout<<"\n Discount :";
cin >> Customer[i].discount;

cout<<"\n Sales Tax :";
cin>> Customer[i].sales_tax;

}

clrscr();

cout<<" \n Customer Num.     Customer Name \t Address \t Date-of-Sale    Desc \t Quantity    Unit Price   Discount%   Sales-Tax% \n\n";

cout<<"================================================================ "

for( i = 0; i< 10; i++)
{
cout<< Customer[i].cust_num << "   "  << Customer[i].cust_nm << "\t" << Customer[i].add << "\t" << Customer[i].D_O_sale.day<<"-"<<  Customer[i].D_O_sale.mon << "-" << Customer[i].D_O_sale.yr << "    "<< Customer[i].descp << "\t" <<  Customer[i].quan << "   "<<   Customer[i].unit_pr << "    "<<  Customer[i].discount << "    "  << Customer[i].sales_tax << endl ;
}

getch( );
}

Friday, January 22, 2010

Create a structure called volumn that uses three variables (lenght, width, height ) of type distance (feet and inches) to model the volume of a room. Read the three dimensions of the room and calculate the volumn it represents , and print out the result. The volumn should be in cu. feet form, i.e., you will have to convert each dimension in to feet and fractions of foot. For instance , the lenght 12 feet 6 inches will be 12.5 feet.



#include<iostream.h>
#include<conio.h>

struct vol {
int  l_in_feet, b_in_feet, h_in_feet;
int l_in_inch, b_in_inch, h_in_inch;
};

void main()
{
vol room;
char ch[10];
cout << “\n Enter  length in feet and inches(XfeetXinch/X F. X I.) :”
cin >> l_in_feet >> ch >> l_in_inch >> ch;
cout << “\n Enter breadth  in feet and inches (XfeetXinch..):”
cin >> b_in_feet >> ch >> b_in_inch >> ch;
cout << “\n Enter  height in feet and inches(XfeetXinch…)  :”
cin >> h_in_feet >> ch >> h_in_inch >> ch;

float len, br, height;

len = l_in_feet + (float)(l_in_inch/12);
br = b_in_feet + (float)(b_in_inch/12);
height = h_in_feet + (float)(h_in_inch/12);
double  volm = (double)len * br*height;

cout<< “\n The Length  of the Room = “ << len << “feet”;
cout<< “\n The Breadth of the Room = “ << br << “feet”;
cout<< “\n The Volume of the Room = “ << height << “feet”;
cout<< “\n The Volume of the Room = “ << volm << “c.feet”;
getch();
}

Consider the following structure :

struct info {
float hra_rate, da_rate, cca, pf_rate, it_rate;
int days_worked;
};

struct emp
{
int empno;
char name[21];
float basic;
info others;
} salemp, puremp, imemp;

Using the information stored in others member of emp, calculate the Gross and Net Salary of an employee. The Gross Salary is calculated as follows :

(basic x days _ worked) /26  + (basic x hra _ rate)/100 + (basic x da _rate)/100 +cca

The Net Salary is calculated as follows :

Gross_Salary – ((basic x it _rate) /100 + (basic x pt _rate )/100)

==================================================================

#include<iostream.h>
#include<conio.h>
#include <stdio.h>

struct info {
float hra_rate, da_rate, cca, pf_rate, it_rate;
int days_worked;
};


struct emp
{
int empno;
char name[21];
float basic;
info others;
} salemp, puremp, imemp;




void main()
{
float gs, ns;
cout<< “\n Enter Records for Sale Emp :”;
cout <<”\n Enter EmpNo, EmpName, Basic Pay, HRA-rate, DA-rate, CCA-rate, PF-rate, IT-rate, Days of Work :”;
cin >> salemp.empno;
gets(salemp.name);
cin>> salemp.basic>> salemp.others.hra_rate >> salemp.others.da_rate >> salemp.others.cca >> salemp.others.pf_rate >> salemp.others.it_rate;
gs = (salemp.basic x salemp.others.days _ worked) /26  + (salemp.basic x salemp.others.hra _ rate)/100 + (salemp.basic x salemp.others.da _rate)/100 + salemp.others.cca;
ns = gs – ((salemp.basic x salemp.others.it_rate)/100 +( salemp. basic x salemp.others.pt_rate) /100);

cout<< “\nEmpNo.\tName\tThe Gross salary (Rs.)\t The Net Salary (Rs).”;

cout<<”\n”<< salemp.empno << “\t” << salemp.name<<”\t”<
<<”\t”< < gs < < ns;
cout<<”\n*******************************************”;




cout<< “\n Enter Records for Purchase Emp :”;
cout <<”\n Enter EmpNo, EmpName, Basic Pay, HRA-rate, DA-rate, CCA-rate, PF-rate, IT-rate, Days of Work :”;
cin >> puremp.empno;
gets(puremp.name);
cin>> puremp.basic>> puremp.others.hra_rate >> puremp.others.da_rate >> puremp.others.cca >> puremp.others.pf_rate >> puremp.others.it_rate;


gs = (puremp.basic x puremp.others.days _ worked) /26  + (puremp.basic x puremp.others. hra _ rate)/100 + (puremp.basic x puremp.others.da _rate)/100 + puremp.others.cca;


ns = gs – ((puremp.basic x puremp.others.it_rate)/100 +( puremp.basic x puremp.others.pt_rate) /100);
cout<< “\nEmpNo.\tName\tThe Gross salary (Rs.)\t The Net Salary (Rs).”;
cout<<”\n”<< salemp.empno << “\t” << salemp.name<<”\t”<
<<”\t”< < gs < < ns ;
cout<<”\n*****************************************”;
getch();

Monday, January 18, 2010

A phone number such as (011)711 8802 can be thought of as having three parts: the area code(011), the exchange(711), and the number(8802). Write a program that uses a structure to store these three parts of a phone number separately.Call the structure phone. Create an array to store 20 records of its member wherein each record stores the member no., member name and phone number of phone type. Have the user input the information for all records and then display the entire information on the screen.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>

struct phone{
int area_code;
int exchange;
int number;
};

struct member{
int member_id;
char name[30];
phone ph_no;
};

void main()
{
member rec[20];
char ch;
cout<<"\n Enter records of 20 members ";

for(int i = 0; i < 20 ; i++)
{
cout<< "\n Member ID ? ";
cin >> rec[i].member_id;
cout<< "\n Enter Name :";
gets( rec[i].name);
cout<< " \n Enter phone number in the format \'areacode - exchange - number (003 - 2442 - 6523)\' :";
cin >> rec[i].ph_no.area_code >> ch >>  rec[i].ph_no.exchange >> ch >> rec[i].ph_no.number ;
}

clrscr();

cout<< "\n Member ID \t Member Name \t Ph. No. \n ";
cout<< "--------------------------------------------\n\n";
for(int i = 0; i < 20 ; i++)

{
cout<< rec[i].member_id << "\t" << rec[i].name << "\t" << rec[i].ph_no.area_code<< "-" <<  rec[i].ph_no.exchange <<  "-" <<  rec[i].ph_no.number << endl ;
}

getch();

}











Write a program to record score of a cricket match. One array stores information of batting team such as bat'sman name, runs scored, indication if out, mode by which out along with total runs, overs played, total overs and extras. The other array stores information about bowling team such as bowler's name, over's bowled, maiden overs, runs given, and wicked taken. The program reads in the above information and depending upon the user's choice, it displays either the batting team's information or the bowling team's information.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>




struct batting
{
char batsman[20];
int run;
boolean out;
int tot_run;
int overs;
int total_over;
int extra;
};




struct bowling
{
char bowler[20];
int overs;
int maiden_overs;
int runs;
int wicket;
};


void main()
{
batting bat_team[12];


bowling bo_team[12];


cout<<"\n Enter records of batting team :\n\n";
for(int i = o; i < 12 ; i++)
{
cout<<"\n Enter bat's man name :";
gets(bat_team[i].batsman);
cout<<"\n Enter run scored :";
cin>>bat_team[i].run;
cout<<"\n Type 0 NOT-OUT , Type 1 if OUT :";
cin>>bat_team[i].out;
cout<<"\n Enter total run :";
cin>>bat_team[i].total_run;
cout<<"\n Enter no. of overs played :";
cin>>bat_team[i].overs;
cout<<"\n Enter total over:";
cin>>bat_team[i].total_over;
cout<<"\n Extra ?";
cin>>bat_team[i].extra;
}


cout<<"\n Enter records of bowling team :\n\n";


for( i = o; i < 12 ; i++)
{
cout<<"\n Enter bowler's name :";
gets(bo_team[i].bowler);
cout<<"\n Enter overs bowled :";
cin>>bo_team[i].overs;
cout<<"\n Enter maiden overs :";
cin>>bo_team[i].maiden_overs;
cout<<"\n Enter runs :";
cin>>bo_team[i].runs;
cout<<"\n Enter wicked taken:";
cin>>bo_team[i].wicket;
}






char ch;
int n;


do
{
clrscr();


cout<<"\n Enter 1 to view batting team information:";


cout<<"\n Enter 2 to view bowling team information:";


cout<<"\n Enter your choice :";


cin>>n;


switch(n)


{


case 1:


cout<<"\n ************* BATTING TEAM *************\n";


cout<<"\n Name \t RUN-Scored Out Total-run Over's-played Total-Overs Extra ";


for( i = o; i < 12 ; i++)


{


cout<<"\n"<< bat_team[i].batsman <<"\t"<< bat_team[i].run<<" ";


if(bat_team.out)


cout<<"OUT";
else
cout<<"NOT-OUT";


cout<< " " <<   bat_team[i].total_run<< " " << bat_team[i].overs << " " << bat_team[i].total_over<<  " "<< bat_team[i].extra;
}


break;




case 2:


cout<< "\n ************* BOWLING TEAM *************\n";
cout<< "\n Name \t Overs_bowled Maiden-Overs Runs-Given Wicket-Taken ";


for( i = o; i < 12 ; i++)


{
cout<<  "\n"<< bo_team[i].bowler<< "\t"<< bo_team[i].overs<< " ";
cout<< " "<< bo_team[i].maiden_overs<< " "<< bo_team[i].runs<< " "<< bo_team[i].wicket<<" ";
}
break;


default:




cout<< "\n Wrong choice.";
 
}
 
cout<<"\n Do you continue (y/n);


cin>>ch;


}while(ch=='y' | | ch == 'Y');
getch();
}

Declare a structure to represent a complex no. ( a no. having a real and imaginary part). Write C++ function to add, subtract, multiply and divide two complex nos.


#include<iostream.h>
#include<conio.h>


struct complexNo
{
float real;
float img;
};
typedef complexNo cm;


void main()
{
cm n1, n2, res;


cout<<"\nEnter 2 complex nos:";


cout<<"\n\n Enter 1st No.:";


cout<<"\n Real = ? ";


cin>>n1.real;


cout<<"\n Imaginary = ? ";


cin>>n1.img;


cout<<"\n Enter 2st No.:";


cout<<"\n Real = ? ";


cin>>n2.real;


cout<<"\n Imaginary = ? ";


cin>>n2.img;

 res.real = n1.real +n2.real;


res.img = n1.img + n2.img;


cout<<"\n Sum of " << n1.real << " + i " << n1.img << " & " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img;


res.real = n1.real -n2.real;


res.img = n1.img - n2.img;


cout<<"\n Diff. of " << n1.real << " + i " << n1.img << " & " << n2.real << " + res.img; 


res.real = n1.real *n2.real;
res.img = n1.img *n2.img;

cout<<"\n Product of " << n1.real << " + i " << n1.img << " & " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img; 


res.real = n1.real / n2.real;
res.img = n1.img / n2.img;


cout<<"\n Result od dividing of " << n1.real << " + i " << n1.img << " by " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img; 


getch();
}


Friday, January 15, 2010

Write a program, which will read in student records and print them in sorted order. Each record should be represented by a C structure having member/fields for the first name, last name and student number(an integer). You should sort first by first name, then by last name (this way they will be fully sorted by name when you finish). You can read the information in from the console or from a file, and should print out the sorted list to the console. You can assume some reasonable upper limit on number of records (say , 25) if you want to use an array.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

// program is done by console input and the array with upper limit 25



struct student
{
char fname[20];
char lname[20];
int id_no;
};
typedef student S;
void main()
{
S s[25];

char tfname[20], tlname[20];
int t;
for(int i = 0; i<25; i++)
{

cout<<"\n Enter Student's first name:";
cin>>s[i].fname;
cout<<"\n Enter Student's last name:";
cin>>s[i].lname;
cout<<"\n Enter ID NO";
cin>>s[i].id_no;
}

for(i = 0; i<24; i++)

{
for(int j = i+1; j<25; j++)
{
if(strcmp(s[i].fname, s[j].fname)>0)
{
tfname = s[i].fname;
s[i].fname = s[j].fname;
s[j].fname = tfname;

tlname = s[i].lname;

s[i].lname = s[j].lname;
s[j].lname = tlname;

t = s[i].id_no;
s[i].id_no = s[j].id_no;
s[j].id_no = t;
}
else
{
 if(strcmp(s[i].fname, s[j].fname)==0)

  {
       if(strcmp(s[i].lname, s[j].lname)>0)        {tfname = s[i].fname;


         s[i].fname = s[j].fname;
         s[j].fname = tfname;

         tlname = s[i].lname;
         s[i].lname = s[j].lname;
         s[j].lname = tlname;


         t = s[i].id_no;
         s[i].id_no = s[j].id_no;
         s[j].id_no = t;
        }
}
}
}

cout<<"\n\n FIRST NAME \t LASTNAME \t ID NO ";

for(int i = 0; i<25; i++)

{
cout<<"\n"<< c[i].fname<<"\t" <lt; c[i].lname <<="" <
}
getch();
}



 



A Linear array if size 50 stores following informations : name the country, country's capital and per capita income of the country. Write a complete program in C++ to do the following:

(a) To read a country's name and display capital and per-capita income.
(b) To read name of the capital city and displays country's name  and displays country's name and per capita income.
Display an error message in case of an incorrect input.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

struct country{
char country_name[20];
char capital_name[20];
float incm;
};

typedef country C;

void main()
{

C c[50];

for(int i = 0; i<49; i++)
{
cout<<"\n Enter Country's name :";
cin>>c[i].country_name;

cout<<"\n Enter the name of Capital :";
cin>>c[i].capital_name;

cout<<"\n Enter per capita incme:";
cin>>c[i].incm;

}

char cname[20];
char capital[20];
char choice;
int n, flag = 0 ;
do
{

cout<<"\n ***********MENU***********\n\n";
cout<<" 1. Know Capital for a country :";
cout<<"\n 2. Know County for a capital :";

cout<<"\n\n Enter your choice :";
cin>>n;

switch(n)
{
case 1:

cout<<"\n Enter a country name :";

gets(cname);
for(i=0; i<50; i++)
if(strcmp(c[i].country_name, cname)==0)
{ 
cout<<"The capital of  "<< c[i].country_name <<" is " << c[i].capital_name ;
cout<<"\n Per caita income = Rs. "<< c[i].incm;
flag = 1;
break;
}
break;

case 2:cout<< "\n Enter a capital name :";

gets(capital);
for(i=0; i<50; i++)
if(strcmp(c[i].capital_name, capital)==0)
{
cout<<"\nThe capital of "<< c[i].capital_name  <<" is " << c[i].country_name ;
flag = 1;
cout<<"\n Per capita income = Rs. "<< c[i].incm;
break;



}
break;

default:
cout<<"\n Wrong Input! ";
}

if(flag == 0)
cout<<"\n No Information .";

cout<<"\n\n Do you want to continue (Y/N) :";
cin>>choice;
}while(choice == 'Y' || choice == 'y');
getch();


}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.