Constants-Data Structure Programming-Lab Task Solution, Exercises of Data Structures and Algorithms

This is code assigned in lab of course. It was assigned by Jay Singh at Aligarh Muslim University for Data Structure course. It includes: Array, Class, Objects, Data, Type, Position, Static, members, Variables, Global, Initialization

Typology: Exercises

2011/2012

Uploaded on 07/11/2012

dhanvant
dhanvant 🇮🇳

4.9

(9)

89 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 1 Solutions
6.096 Staff
1 “Hello, World” (10 points)
1.1 Hello World I (1 point)
1 # i nc lu d e < io s tr e am >
2
3 us in g names pa ce s td ;
4
5 int mai n ()
6 {
7 co ns t ch ar * str = " H el lo , Wo r ld ! " ;
8 co ut < < st r << " \n " ;
9 return 0;
10 }
1.2 Hello World II (9 points)
1. for loop: (3 points)
1 # i nc lu d e < io s tr e am >
2
3 us in g names pa ce s td ;
4
5 int mai n ()
6 {
7 in t N;
8 ci n >> N ;
9 fo r (; N- - > 0; )
10 {
11 co ut < < "Hello , Wo r ld ! \ n ";
12 }
13 return 0;
14 }
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Constants-Data Structure Programming-Lab Task Solution and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Lab 1 Solutions

6.096 Staff

1 “Hello, World” (10 points)

1.1 Hello World I (1 point)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 const char * str = " Hello , World! " ; 8 cout << str << " \ n " ; 9 return 0; 10 }

1.2 Hello World II (9 points)

1. for loop: (3 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 for (; N - - > 0;) 10 { 11 cout << " Hello , World !\ n " ; 12 } 13 return 0; 14 }

2. while loop: (3 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 while (N - - > 0) 10 { 11 cout << " Hello , World !\ n " 12 } 13 return 0; 14 }

3. do...while loop: (3 points)

include < iostream >

using namespace std ;

int main () { int N ; cin >> N ; do { cout << " Hello , World !\ n " ; } while ( - - N > 0) ; return 0; }

29 maxVal = a ; 30 } 31 } 32 33 cout << " Mean : " << ( double ) acc / N << " \ n " ; 34 cout << " Max : " << maxVal << " \ n " ; 35 cout << " Min : " << minVal << " \ n " ; 36 cout << " Range : " << ( maxVal - minVal ) << " \ n " ; 37 38 return 0; 39 }

2.3 Prime Numbers (10 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 int N ; 8 cin >> N ; 9 for ( int i = 2; N > 0; ++ i ) 10 { 11 bool isPrime = true ; 12 for ( int j = 2; j < i ; ++ j ) 13 { 14 if ( i % j == 0) 15 { 16 isPrime = false ; 17 break ; 18 } 19 } 20 if ( isPrime ) 21 { 22 --N ; 23 cout << i << " \ n " ; 24 } 25 } 26 return 0; 27 }

2.4 Multiples of numbers (10 points)

2.4.1 Ternary operator (3 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 while (1) 8 { 9 int N ; 10 cin >> N ; 11 cout << (( N % 5 == 0 && N >= 0)? N /5 : -1) << " \ n " ; 12 } 13 return 0; 14 }

2.4.2 continue (3 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 int main () 6 { 7 while (1) 8 { 9 int N ; 10 cin >> N ; 11 if ( N % 5 > 0) 12 { 13 cout << " -1\ n " ; 14 continue ; 15 } 16 cout << N /5 << " \ n " ; 17 } 18 return 0; 19 }

2.4.3 break (3 points)

3 Factorials Gone Wrong (40 points)

3.1 Writing the factorial program (5 points)

3.2 Breaking the program (5 points)

If − 1 is entered, the program will output 1, which is incorrect because the factorial function

(or Gamma function) is not defined for negative numbers.

3.3 Breaking the program II (5 points)

The number at which the program fails depends on the system architecture; 64-bit systems

often fail at 17. We accepted any answer around that value.

3.4 Rewriting Factorial (10 points)

1 # include < iostream > 2 3 using namespace std ; 4 5 long long accumulator = 1; 6 7 int main () 8 { 9 int number ; 10 cout << " Enter a number : " ; 11 cin >> number ; 12 if ( number < 0) 13 { 14 cout << " No negative numbers allowed !\ n " ; 15 return 1; 16 } 17 if ( number > 20) 18 { 19 cout << " Program will not produce correct result !\ n " ; 20 } 21 for (; number > 0; accumulator *= number - -) ; 22 cout << " The factorial of " << number << " is " << accumulator << " .\ n " ; 23 return 0; 24 }

3.5 Rewriting Factorial II (10 points)

include < iostream >

using namespace std ;

int main () { int number ; cout << " Enter a number : " ; cin >> number ; switch ( number ) { case 0: case 1: cout << " 1\ n " ; break ; case 2: cout << " 2\ n " ; break ; case 3: cout << " 6\ n " ; break ; case 4: cout << " 24\ n " ; break ; case 5: cout << " 120\ n " ; break ; case 6: cout << " 720\ n " ; break ; case 7: cout << " 5040\ n "; break ; case 8: cout << " 40320\ n " ; break ; case 9: cout << " 362880\ n " ; break ; case 10: cout << " 3628800\ n " ; break ; default :