Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem #1:
- Write a program that creates an array of 5 integer values using a for loop, 1 through
- 5. The iterator can be used as the values to be stored. The program should
- then use a second for loop to copy those elements into a second array, but this time
- you should use a for loop to enter the values into the new array backwards. The
- program should then use a third for loop to print out the first array to screen on one
- line, then a fourth for loop to print out the second array. The output should
- resemble this:
- A: 1 2 3 4 5
- B: 5 4 3 2 1
- /*** SKELETON CODE ***/
- /*
- * This program demonstrates how to copy one array to
- * another array and how to make use of the iterator.
- * By: L. Snedden
- */
- #include <stdio.h>
- int main(void)
- {
- int a[5] = {0};
- int b[5] = {0};
- //FIRST LOOP GOES HERE (This one initializes the values)
- //SECOND LOOP GOES HERE (This one copies)
- //THIRD LOOP GOES HERE (This one prints the first array)
- //FOURTH LOOP GOES HERE (This one prints the second array)
- return 0;
- }
- Problem #2:
- Write a function that reverses the elements of an array of integers so that the last
- element becomes the first, the second from last becomes second, and so forth. The
- function is to reverse the elements in place, i.e., WITHOUT USING A SECOND ARRAY. You
- can use a variable to hold an element temporarily. Try changing up your program to use an odd-sized
- array or an even-sized array to test it out!
- /*** SKELETON CODE ***/
- #include <stdio.h>
- //Function Prototype
- void reverseArray(int[], int);
- int main(void)
- {
- //local declarations
- int ary[10];
- /*** INITIALIZE ARRAY VALUES HERE (Using a Loop) ***/
- printf("Before reversal:\n");
- //Loop to Print all values
- for(int i = 0; i < 10; i++) {
- printf("Element %d is %d\n", i, ary[i]);
- }
- /*** CALL YOUR FUNCTION HERE ***/
- printf("\nAfter reversal:\n");
- //Loop to Print values AFTER reversal
- for(int i = 0; i < 10; i++) {
- printf("Element %d is %d\n", i, ary[i]);
- }
- return 0;
- }//end main
- /* REVERSE ARRAY FUNCTION */
- void reverseArray(int x[], int size)
- {
- /*** LOOP GOES HERE ***/
- return;
- }//end function
- Problem #3:
- The International Standard Book Number, ISBN, is used to uniquely identify a book.
- It is made of 10 digits. Write a function that tests an ISBN to see if it is valid. For an
- ISBN number to be valid, the weighted sum of the 10 digits must be evenly divisible
- by 11.
- To determine the weighted sum, the value of each position is multiplied by its
- relative position, starting from the right, and the sum of the product is determined.
- Since the weighted sum modulus 11 is 0, the ISBN number is valid. Test your
- function with examples, use Amazon to get valid book ISBNs if you want.
- Example
- ISBN: 0-07-881809-5 (assume no dashes in your function)
- Code Weight Value
- 0 10 0
- 0 9 0
- 7 8 56
- 8 7 56
- 8 6 48
- 1 5 5
- 8 4 32
- 0 3 0
- 9 2 18
- 5 1 5
- Weighted Sum is 220
- 220 % 11 = 0 (Valid ISBN)
- SAMPLE OUTPUT:
- Enter in 10 numbers one at a time: 0
- Enter in 10 numbers one at a time: 0
- Enter in 10 numbers one at a time: 7
- Enter in 10 numbers one at a time: 8
- Enter in 10 numbers one at a time: 8
- Enter in 10 numbers one at a time: 1
- Enter in 10 numbers one at a time: 8
- Enter in 10 numbers one at a time: 0
- Enter in 10 numbers one at a time: 9
- Enter in 10 numbers one at a time: 5
- The weighted sum is 220
- The ISBN is valid
- /*** SKELETON CODE ***/
- /* This program is a test driver for the ISBN
- * validation function. This function tests the
- * weighted sum and value to determine if the
- * ISBN number is valid.
- */
- #include <stdio.h>
- //Function prototype
- int isbnTest(int[]);
- int main(void)
- {
- //local declarations
- int ary[10];
- int result;
- //local statements
- //Loop to scan 10 values from user one at a time (NO DASHES)
- for(int i = 0; i < 10; i++)
- {
- printf("Enter in 10 numbers one at a time: ");
- scanf("%d", &ary[i]);
- }
- /*** CALL FUNCTION AND CHECK VALIDITY HERE ***/
- return 0;
- }//end main
- //ISBN TEST FUNCTION
- int isbnTest(int ary[])
- {
- /*** WRITE FUNCTION BODY HERE (Variables, Loop, etc) ***/
- }//end fuction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement