Solution:
#include<iostream.h>
void main() {
cout << "Hello World!" ;
}
f = 32 + (9/5) c
Solution:
#include<iostream.h>
void main() {
double c ;
cin >> c ;
cout << ( 32.0 + (9.0/5.0) * c ) << endl ;
}
#include<iostream.h>
void main()
{
char c ;
for ( c='a'; c<='z'; c++ )
{
cout << c << endl ;
}
}
Write a C++ program which prints all the two letter
combinations aa, ab, ac, etc., through zz each on a single
line.
Solution:
#include<iostream.h>
void main() {
char c ;
for ( c='a'; c<='z'; c++ ) {
char d ;
for ( d='a'; d<='z'; d++ ) {
cout << c << d << endl ;
}
}
}
#include<iostream.h>
#include<strings.h>
void rvrStr(char * s ) {
// you write the code here
}
void main() {
char * s = "able was i" ;
rvrStr(s) ;
cout << s << endl ;
}
prints out i saw elba.
Solution:
This was presented in class as program
SereverSenil.C
and available with the usual class materials.
Here is another possibility:
void rvrStr(char * s ) {
char * p ;
p = s + strlen(s) - 1 ;
while (p>s) {
char t ;
t = *s ;
*s = *p ;
*p = t ;
p-- ;
s++ ;
}
}
int findInt( int x, int A[], int n ) {
// body of function, you do it.
}
Solution:
int findInt( int x, int A[], int n )
{
int i ;
for ( i=0; i<n; i++ ) {
if ( x==A[i] ) return i ;
}
return -1 ;
}
struct N {
int theInt ;
N * next ;
}
Example. if N * p points to the three element list containing
integers 3, 4 and -1 it returns 6.
Solution:
int sumLL(N * n)
{
int sum = 0 ;
while ( n!=NULL ) {
sum += n->theInt ;
n = n->next ;
}
return sum ;
}
N * bePos(N * l) {
// your write the function
}
which takes a linked list of integers, using the definition
of a linked list given in the previous problem, and
returns the linked list remaining after removing all the
nodes whose theInt values are negative.
Solution:
N * bePos(N * l)
{
// assume dummy header
Node * p = l ;
while ( p->next!=NULL ) {
if ( p->next->theInt < 0 ) {
// remove
p->next = p->next->next ;
}
else {
// keep
p = p->next ;
}
return l ;
}