Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..

Similar presentations


Presentation on theme: "Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer.."— Presentation transcript:

1 Computer Programming Arrays 1

2 Question #1 2 Question Choose the correct answer..

3 1.Which of the following correctly declares an array? A. int anarray[10]; B. int anarray; C. anarray{10}; D. array anarray[10]; 3 Question #1 Question Answer

4 2. What is the index number of the last element of an array with 29 elements? A. 29 B. 28 C. 0 D. Programmer-defined 4 Question #1 Question Answer

5 3. Which of the following is a two- dimensional array? A. array anarray[20][20]; B. int anarray[20][20]; C. int array[20, 20]; D. char array[20]; 5 Question #1 Question Answer

6 4. Which of the following correctly accesses the seventh element stored in foo, an array with 100 elements? A. foo[6]; B. foo[7]; C. foo(7); D. foo; 6 Question #1 Question Answer

7 5. If char catname[15];, which of the following is valid ? A. catname[15] = 'milie'; B. catname = 'milie'; C. catname[] = 'milie'; D. none are valid. 7 Question #1 Question Answer

8 8 Question #2 Question * Write one or more statements that perform the following tasks for an array called fractions:

9 9 Question #2 Question Answer Const int arraySize=10; Define a constant integer variable arraySize initialized to 10.

10 10 Question #2 Question Answer Double fractions[arraySize]={0.0}; Declare an array with arraySize elements of type double, and initialize the elements to 0.

11 11 Question #2 Question Answer fractions[9]=1.667; Assign the value 1.667 to array element 9.

12 12 Question #2 Question Answer fractions[6]= 3.333; Assign the value 3.333 to the seventh element of the array.

13 13 Question #2 Question Answer Cout<<" elements 6 ="<< fractions[6] <<" elements 9="<< fractions[9]; Print array elements 6 and 9.

14 14 Question #2 Question Answer for ( int i = 0; i < arraySize; ++i ) cout << "fractions[" << i << "] = " << fractions[ i ] << endl; Print all the array elements using a for statement. Define the integer variable i as a control variable for the loop.

15 15 Question #3 Question Trace the following programs, and show the output:

16 16 Question #3 int array [] = {16, 2, 3, 4, 8}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += array[n]; } cout << result << endl; return 0; }

17 17 Question #3 Answer resultArray [n]nLoop # 16 01 18212 21323 25434 33845 Press any key to continue...

18 18 Question #3 char question[] = "Please, enter your first name: "; char greeting[] = "Hello, "; char yourname [80]; cout << question; cin >> yourname; cout << greeting << yourname << "!" << endl;

19 19 Question #3 Answer Please, enter your first name: Hadeel Hello, Hadeel ! Press any key to continue...

20 20 Question #4 Question Find the error(s) in each of the following statements:

21 21 Question #4 Question Answer Index out of bound If they meant to print the three numbers in the array, then they should have used a[0], a[1], a[2] Assume that: int a[ 3 ]; cout << a[ 1 ] << " " << a[ 2 ] << " " << a[ 3 ] << endl;

22 22 Question #4 Question Answer Too many initializers - at most 3 if they've declared f[3] double f[4]= {1.1,10.01,100.001,1000.0001}; double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 };

23 programming 23

24 24 Problem #1 Question Write a program that calculates the number of occurrences of an integer x in an array of n integers. How many integers you want to enter: 8 Enter the numbers: 10 8 200 8 1 0 5 8 Which number you want to find its occurrences: 8 8 occurs 3 times

25 25 Question #1 Answer

26 26 Problem #1

27 27 Problem #2 Question Write a C++ program that reads 10 numbers from the user to initialize one dimensional array. Then if the number is greater than or equal to 10, convert it to 1, otherwise convert it to 0. please Enter (10) numbers: 10 8 200 8 1 0 5 8 77 48 The array is : 1 0 1 0 0 0 0 1 1

28 28 Question #2 Answer

29 29 Question #2 Answer

30 30 Problem #3 Question Write a program that asks the user to type 10 integers of an array. The program must output the largest element in the array, and the index at which that element was found Enter 10 integer:1 2 5 8 12 56 13 5 7 8 The largest number is 56 at index 5

31 31 Question #3 Answer

32 32 Question #3 Answer

33 33 Problem #4 Question Write a program that reads a string then check if the string is Palindrome. NOTE: - a palindrome is a string that is spelled the same way forward and backward. for example, “radar” “madam” “abba” are palindromes, but “car” is not. - Your string should be declared as an array of char.

34 34

35 35 Problem #4

36 Evaluation 36

37 37 Problem Question Write a program that reads an array of 10 integers from the user, and then find the sum of the numbers that divisible by 3.

38 38

39 39


Download ppt "Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer.."

Similar presentations


Ads by Google