Sie sind auf Seite 1von 12

Assignment Pointers

Output and Errors


(a) void main()
{
int A[]={12,18,20, 35,40};
int *p=A;
while(*p<30)
{
if(*p%3!=0)
*p=*p+5;
else
*p=*p+2;
p++;
}
for(int j=0;j<=4;j++)
{
cout<<A[j]<<"*";
if(j%3==0)
cout<<endl;
}
cout<<A[4]*3<<endl;
}
Output
14*
20*25*35*
40*120
(b) #include <iostream.h>
struct School
{
int year;
float topper;
};
void change( School *s, int x=10)
{
s->topper=(s->topper+25)-x;
s->year++;
}
void main()
{
School arr[]={{2008,150},{2009,98})
School *pointer=arr;
change(pointer,100);
cout<<arr[0].year<<- <<arr[0].topper<<endl;
change(++pointer);
cout<<pointer->year<<- <<pointer->topper<<endl;
}
1

Output
2009- 75
2010 - 113
Rewrite the following program after removing all the syntactical errors underlining
each correction. (if any)
include<iostream.h>
struct
{ int x=10;
float y;
}A,*p;
void main( )
{
p=A;
cout<<\nInput Integer : ;
cin>>p.x;
cout<<\nInput any number : ;
cin>>A.y;
}
(d) What will be the out put of the following program?
#include <iostream.h>
void RIDDLE(int *N,int c)
{ for(int i=1;i<c;i++)
*(N+i-1)=*(N+i)+1;
}
void main()
{
int p[ ] = {6,9,8},q[ ]={4,3,1},r[]={50,80};
clrscr();
JEESHA PRAMOD CHINMAYA VIDYALAYA KANNUR Page 2
RIDDLE(p,3);
RIDDLE(q,3);
RIDDLE(r,2);
for(inti=0;i<3;i++)
cout<<p[i]<<-;
cout<<endl;
for(i=0;i<3;i++)
cout<<q[i]<<%;
cout<<endl;
for(i=0;i<2;i++)
cout<<r[i]<<-;
cout<<endl;
}
Output
10-9-84%2%1%
81-80(e) void main( )
2

{
char * NAME = admiNStrAtiOn;
for( int x=0;x<strlen(NAME);x++)
if(islower(NAME[x]))
NAME[x] = toupper(NAME[x]);
else
if(isupper (NAME[x]))
if(x%2==0)
NAME[x] = NAME[x -1];
else
NAME[x]--;
cout<<NAME <<endl;
}
Output
ADMIIRTRRTINN
(f) void main( )
{
char *poet= "SakESpHerE" ;
for (int i=0;i<strlen(poet);i++)
{
if(islower(poet[i]))
poet[i]=poet[i-1];
else if( isupper(poet[i]))
if(poet[i]= ='S')
poet[i]='X';
else if(poet[i]= ='E')
poet[i]=toupper(poet[i-1]);
else
poet[i]--;
}
cout<<poet; }
Output
XXXXXXGGGG
(g) #include<iostream.h>
void main()
{
int Numbers[] = {2,4,8,10};
int *ptr = Numbers;
for (int C = 0; C<3; C++)
{
cout<< *(ptr+1)-1 << @;
ptr++;
}
cout<<endl;
for(C = 0; C<4; C++)
{
(*ptr)=(*ptr * 2);
3

--ptr;
}
for(C = 0; C<4; C++)
cout<< Numbers [C]<< #;
cout<<endl;
}
Output
3@7@9@
4#8#16#20#
(h)

#include<iostream.h>
void main()
{
int list[5];
*list=5;
for(int i=1; i<5;i++)
*(list+i)= *(list+i-1)*i;
cout<<\n;
for(i=0;i<5;i++)
cout<<<<*(list+i);
}
Output
5 5 10 120
(i) Rewrite the following program after removing all syntactical error(s), if any.
Underline each correction.
$include<iostream.h>
class ERROR
{
char E_Name[20];
int E_No=10;
public:
ERROR( ) { };
void show_ERROR( )
{
gets(E_Name); cin>>E_No;
}
};
void main( );
{
ERROR *E;
4

E.show( );
}

(j) #include<iostream.h>
int main( )
{
void NewValue(int, int *);
int val[ ] = {3, 5, 7, 9, 11};
int i, num=5;
for( i = 0 ; i < 5 ; i++)
{
NewValue(val[i], &num);
cout<<val[i] << \t << num << \n;
}
}
void NewValue( int A, int *B)
{
A = *(B) += 3;
}
Output
38
5
11
7

14

17

11

20

(k)

#include<iostream.h>
#include<ctype.h>
void main( )
{
5

char *s = Polymorphism;
int L = strlen ( s );
for ( int C = 0 ; C < L ; C ++)
{
if (islower ( s [C] )
s[C] = toupper( s[C]);
else if (C % 2 == 0)
s[C] = E;
else
s[C] = tolower( s[C]);
}
cout<<s;
}
Output
EOLYMORPHISM
(l) Rewrite the following program after removing the syntactical error(s), if any.
Underline each correction:
# include <iostream.h>
# include <conio.h>
# define pi = 3.1415
# define square(x) = x*x
const float r 1.2;
void Main ()
{ clrscr();
float *Area;
float *R;
float area = pi * square (r);
cout << endl << " area " << area;
Area = &area;
*Area *= area;
cout << endl << " area " << area;
R = &r;
*R *= r;
cout << endl << " r " << r;
getch();}
(m)

#include<iostream.h>
#include<ctype.h>
void Modify( char *, int );
void main( )
6

{
char * Disease= SwiNe FLuE;
Modify( Disease, 1 );
cout<<Disease;
}
void Modify ( char * D, int N)
{
for( int i=0; D[i]!=\0, ++i)
if(i % 2= = 0)
D[i]=toupper(D[i]);
else if ( isupper(D[i]))
D[i]= tolower(D[i]);
else if (islower(D[i]))
D[i]= D[i] + N;
}
Output
SWjNf fLvE
(o) #include<iostream.h>
#include<ctype.h>
void main( )
{
char Mystring[ ] = "what@OUTPUT!";
for(int I=0; Mystring [I] ! = '0' ; I++)
{
if(!isalpha (Mystring[I]) )
Mystring[I ]= ' * ';
else if(isupper (Mystring[I] ))
Mystring[I] = Mystring[I]+1;
else
Mystring[I] = Mystring[I+1];
}
cout<<Mystring;
}
Output
hat@*PVUQVU*
(p) #include<iostream.h>
#include<string.h>
#include<ctype.h>
7

void main()
{
char *ch = Our 3 Boys;
for(int p=0;p<strlen(ch);p++)
if(isdigit(ch[p]))
ch[p] = -;
else if(isupper(ch[p]))
ch[p] = tolower(ch[p]);
else if(islower(ch[p]))
ch[p] = toupper(ch[p+1]);
else if(ch[p] == )
ch[p] = *;
cout<<ch<<endl;
}
Output
oR *-*bYS
(q) #include<iostream.h>
int a = 10;
void main()
{
void demo(int &, int, int*);
int a = 20,b = 5;
demo(::a, a, &b);
cout<<::a<<\t<<a<<\t<<b<<endl;
}
void demo(int &x, int y, int *z)
{
a += x;
y *= a;
*z = a + y;
cout<<x<<\t<<y<<\t<<*z<<endl;
}
Output
20

400

420

20

20

420

#include <iostream.h>
void displayarr(int *num , int n = 9)
{
for(int i=0;i<n;i++)
cout<<num[i]<<" ";
cout<<endl;
}
void newarr(int *num,int n = 9)
{
for(int i=0;i<n;i++)
{
8

if(num[i] / 100 != 0)
num[i] = num[i] - 100;
else
num[i] = num[i] + 100;
}
}//newarr
void main()
{
int n[] = {182,24,163,45,158,164,75,18,194};
newarr(n,4);
displayarr(n,6);
newarr(n,8);
displayarr(n);
}
Output
82

124

63

145

158

164

182

24

163

45

58

64

175

118

(s) void PRG(int *a,int n)


{ int i,j,temp,sm,pos;
for(i=0;i<n/2;i++)
for(j=0;j<(n/2)-1;j++)
if(*(a+j)>*(a+j+1))
{ temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp; }
for(i=n-1;i>=n/2;i--)
{ sm=*(a+i);
pos=i;
for(j=i-1;j>=n/2;j--)
if(*(a+j)<sm)
{ pos=j;
sm=*(a+j); }
temp=*(a+i);
*(a+i)=*(a+pos);
*(a+pos)=temp;
}
}
void main( )
{
int w[ ]={-4,6,1,-8,19,5},i;
PRG(w,6);
for(i=0;i<6;i++)
cout<<w[i]<<' ';
}
Output
-4

(v)

struct card {

19

-8

194

char suit[10];
int digit;
};
card* cards = new card[52]; // Allocate Memory
void createdeck()
{

char temp[][10] = {"Clubs","Spades","Diamonds","Hearts"};


int i,m=0,cnt=1;
for(i=1;i<=52;i++)
{

strcpy(cards[i].suit,temp[m]);
cards[i].digit=cnt;
cnt++;
if(i % 13 == 0)
{ m++;cnt=1; }

} }

card drawcard(int num)


{

int rndnum;
randomize();
rndnum = random(num)+1;
return (cards[rndnum]);

}
void main()
{
createdeck();
card c;
c = drawcard(39);
if(c.digit > 10 || c.digit == 1)
{
switch(c.digit)
{
case 11:
cout<<"Jack of "; break;
case 12:
cout<<"Queen of "; break;
case 13:
cout<<"King of "; break;
case 1:
cout<<"Ace of ";
} }
else
cout<<c.digit<<" of ";
cout<<c.suit;
delete[] cards; //Deallocate memory
}
Outputs:
i)
Kind of Spades
ii)
Ace of Clubs
iii) Ace of Diamond
iv)
Queen of Hearts
Output
8 of Diamonds
2 of Spades
9 of Spades
2 of Diamonds
10

(x)

# include <iostream.h>
# include <conio.h>
# include <ctype.h>
# include <string.h>
void upper (char *ch)
{ *ch = char(toupper(*ch)); }
void lower (char *ch)
{ *ch = char (tolower(*ch)); }
void change (char *ch)
{ *ch = '#'; }
void next_char (char *ch, int i)
{ *ch = *ch+i; }
void next_char (char *ch)
{ ++*ch; }
void main ()
{ char c;
char email[ ] ="RIQR : 1133 Gole";
int i = 0;
while (i < strlen(email))
{
if ( isdigit(email[i]))
if (i%2==0)
email[i]++;
if ( ! isalnum (email[i]) && !isspace(email[i]) )
email[i] = '!';
if ((isalpha(email[i])) && (i%2==0) )
{
c = email[i];
upper(&c);
email[i] = c;
}
if ( isspace(email[i]))
{
c = email[i];
change(&c);
email[i] = c;
}
if ( isupper(email[i]))
{
c = email[i];
next_char(&c);
if (i%2==1)
lower(&c);
email[i] = c;
}
i++;
11

}
cout << endl << email ;
getch();
}
Output
SjRs#!#1234#HoMe

12

Das könnte Ihnen auch gefallen