CSI 250L Lab 2, Spring 2012
American University of Science and Technology · Programming II Lab · 3 pages · Dr. Saeed Raheel
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
Show the text of this lab
American University of Science & Technology
Department Of Computer Science
CSI 250L – Computer Programming-II Lab
Spring 2011/2012
Lab Work 2
Problem1:
Trace the following programs:
#include<iostream>
using namespace std;
int main()
{
int x=2, y=4;
int *p;
p= &x;
cout<<"the value of x="<<x++<<endl;
(*p)++;
cout<<"the value of x="<<x<<endl;
int *q;
q=&y;
y+=(*p);
--(*q);
(*p)+=(*q);
cout<<"the value of x="<<x<<endl;
cout<<"the value of y="<<y<<endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
void store(int*,int);
void Max(int*,int,int&);
int main()
{int A[10],max;
store(A,10);
for(int i=0;i<10;i++)
cout<<"A["<<i<<"]="<<*(A+i)<<endl;
Max(A,10,max);
cout<<"the maximum value stored in A is:"<<max<<endl;
system("pause");
return 0;
}
1
void store(int *arr,int ds)
{for(int i=0;i<ds;i++)
*(arr+i)= i*2;
}
void Max(int *a,int ds, int &m)
{m=*a;
for(int i=1;i<ds;i++)
if(*(a+i)>m)
m=*(a+i);
}
Problem 2:
Find the errors in the following program and correct them:
#include<iostream>
using namespace std
Struct item
{
char code[6];
char name[20];
float price;
}
int main ()
{
Item X,*Y;
cout<<"enter the data for item X:";
cin>>X;
cout<<"enter the data for item Y:";
cin>>Y.code>>Y.name>>Y.price;
if(X.name>Y->name);
cout<<"first item name is larger";
else
cout<<"second item name is larger";
system("pause");
return 0;
}
2
Problem 3:
Type the following program then apply the below listed steps:
#include<iostream>
using namespace std;
struct student
{
char fn [20];
char ln [20];
float GPA;
};
int main ()
{
student s ;
student *s1 ;
s1=new student;
cout<< "enter the information about student1:\n";
cout<<"Enter the first name:";
cin>>s.fn;
cout<<"Enter the last name:";
cin>>s.ln;
cout<<"Enter the GPA:";
cin>>s.GPA;
cout<< "enter the information about student2:\n";
cout<<"Enter the first name:";
cin>>s1->fn;
cout<<"Enter the last name:";
cin>>s1->ln;
cout<<"Enter the GPA:";
cin>>s1->GPA;
system("pause");
return 0;
}
1. Compile, link then execute the program.
2. Modify the program to check if s and s1 are on probation or not. (GPA<2.0)
3. Modify the program to store the information about N students. (N is the first input by
the user). Then output the name of all students that are on probation.
Problem 4:
Write a program to input a line of text and store it in an array Line. The program should
allow the user to choose from the following menu:
1- Count the number of words in Line.
2- Output the length of Line.
3- Append “***” to Line.
4- Output the content of Line.
5- Exit.
3