COP 3223: C Programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 33

COP 3223: C Programming Spring 2009 Arrays In C – Part 3 PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on
  • Presentation posted in: General

COP 3223: C Programming Spring 2009 Arrays In C – Part 3. Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3223/spr2009/section1. School of Electrical Engineering and Computer Science University of Central Florida.

Download Presentation

COP 3223: C Programming Spring 2009 Arrays In C – Part 3

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cop 3223 c programming spring 2009 arrays in c part 3

COP 3223: C Programming

Spring 2009

Arrays In C – Part 3

Instructor : Dr. Mark Llewellyn

[email protected]

HEC 236, 407-823-2790

http://www.cs.ucf.edu/courses/cop3223/spr2009/section1

School of Electrical Engineering and Computer Science

University of Central Florida


Cop 3223 c programming spring 2009 arrays in c part 3

In this section of notes, we’ll just look at several more examples of using arrays in various applications.

The first application will be to be write a program that reads the values into a 2-d array from a file, and then computes all of the row and columns sums of the table. The size of the matrix will be the first two values in the file.

Example:

More Examples Using Arrays In C

The sum of each row and column in this matrix equals 15.


Cop 3223 c programming spring 2009 arrays in c part 3

For our second application we’ll look at creating a magic square. A magic square is a square matrix that contains each of the numbers 1, 2, 3, ..., n2 exactly once and has the sum of the numbers in each of its rows, columns and main diagonals equal to the same value. The sum of each row, each columns and the main diagonals is called the magic constant and is equal to (n3+n)/2. So, for example, a 5 X 5 magic square should include each of the numbers 1, 2, 3,…, 25 exactly once and the row, column, and diagonal sums should be (53 + 5)/2 = 65.

I want you to go thru this code and figure out the technique that I used to generate the magic square. How did I know where to place each value in the matrix?

More Examples Using Arrays In C


Cop 3223 c programming spring 2009 arrays in c part 3

I used a technique known as the Siamese method which is:

Starting from the central column of the first row with the number 1, the fundamental movement for filling the squares is diagonally up and right, one step at a time. If a filled square is encountered, one moves vertically down one square instead, then continuing as before. When a move would leave the square, it is wrapped around to the last row or first column, respectively.

For magic squares of odd size n constructed using the Siamese method, several factors apply:

Smallest number = 1

Largest number = n2

Middle number = (n2 /2) + 0.5 (real number division here)

The middle number is always on the diagonal bottom left to top right

The largest number is always opposite 1 in an outside column or row.

More Examples Using Arrays In C


Cop 3223 c programming spring 2009 arrays in c part 3

For our second example of using arrays and loops, we’ll generate the first 30 Fibonacci numbers. Fibonacci numbers are a sequence of integers where the each number is the sum of the two preceding numbers. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

We’ll write a program that uses a 1-d array named fibonacci, initialize the first two values in the array and then uses a loop to generate the remaining Fibonacci numbers.

We’ll have our program write output to both the screen and to a file named “fibonacci.dat”. What we’ll do with this output file, is slightly modify the program you wrote for Assignment #3 and determine which of the first 30 Fibonacci numbers are prime.

More Examples Using Arrays In C


Cop 3223 c programming spring 2009 arrays in c part 3

An aside on Fibonacci numbers:

On many plants, the number of petals is a Fibonacci number: buttercups have 5 petals (F(5)) lilies and iris have 3 petals (F(4)); some delphiniums have 8 (F(6)); corn marigolds have 13 petals (F(7)); some asters have 21 (F(8)) whereas daisies can be found with 34 (F(9)), 55 (F(10)) or even 89 (F(11)) petals.

Look at your own hands: You have 2 hands each of which has 5 fingers, each of which has 3 parts separated by 2 knuckles; all Fibonacci numbers!

Some weird/interesting facts about the Fibonacci sequence.

Every third number in the Fibonacci sequence is even.

Every kth number in the sequence is a multiple of F(k). For example F(4) = 3, and F(8) = 31 which is 7X F(4), F(12) = 144 which is 48X F(4).

In the movie “The DaVinci Code” Fibonacci numbers were used as a combination to unlock a safe.

If you take any 3 consecutive Fibonacci numbers, sum them and divide the sum by two, you will always get the third number! Example: 13 + 21 + 34 = 68 and 68/2 = 34!


Cop 3223 c programming spring 2009 arrays in c part 3

I’ve put two versions of this program on the web site for you. This version uses an int type array and prints only the first 30 Fibonacci numbers. The second version uses a double type array and prints the first 80 Fibonacci numbers.


Cop 3223 c programming spring 2009 arrays in c part 3

In cryptography, a Caesar cipher, also known as a Caesar’s cipher, a shift cipher, or Caesar code, is one of the simplest and most widely known encryption techniques and falls in the general category of cyclic substitution ciphers.

Basically, a Caesar cipher is a substitution cipher where each letter in plaintext (the uncoded message) is replaced by a letter some fixed number of positions down the alphabet, typically referred to as the shift factor, the letter in this shifted position becomes the letter in the ciphertext (the coded message).

This encryption technique was named after Julius Caesar, who used it to communicate with his generals. This encryption technique is often incorporated as part of more complex encryption techniques such as the Vigenère cipher and the ROT13 systems.

Case Study – Caesar Cipher


Cop 3223 c programming spring 2009 arrays in c part 3

Example of a Caesar cipher with shift of 4

Encoder

Original alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Shifted alphabet: W X Y Z A B C D E F G H I J K L M N O P Q R S T U V

Plaintext message: I LOVE C PROGRAMMING

Encoded message: E HKRA Y LNKCNWIIEJC

Since: I = E with shift of 4, L = H with shift of 4, . . .

Decoder

Encoded message: E HKRA Y LNKCNWIIEJC

E = I with shift -4, H = L with shift -4, . . .

Decoded message: I LOVE C PROGRAMMING

Case Study – Caesar Cipher


Cop 3223 c programming spring 2009 arrays in c part 3

What we’ll do in this case study is develop two programs, one that encodes text messages using a Caesar cipher and a second program that will decode a message that was encrypted using a Caesar cipher.

Both programs will be rather similar in nature, in that both will use a 2-d array to hold an original alphabet and a number of shifted versions of that alphabet. The encoder program will read a message from the terminal and ask the user to enter the shift amount for the encryption. The encryption program will also read the original alphabet from a file named “alphabet.dat”. It will encrypt the message and write the shift amount and the encoded message to a file named “encoded message.dat”.

Case Study – Caesar Cipher


Cop 3223 c programming spring 2009 arrays in c part 3

The decoder program will read the file named “encoded message.dat” and then using the shift amount that was stored in this file; decode the message back into plaintext.

Both of the programs are a bit larger than most of the programs we’ve seen so far in the course. We will return to this case study later in the semester when we covered functions and we’ll rewrite these two programs and make both cosmetic and functional improvements to the code.

I have not shown all the code for either of these two program in this set of notes. I’ve only printed out some of the code to highlight the approach that was taken to solve the problem. You will need to download the code from our code page in order to see the whole thing! I encourage you to play with this program a bit.

Case Study – Caesar Cipher


Cop 3223 c programming spring 2009 arrays in c part 3

Once the SHIFTS number of shifted alphabets has been generated, print them out to see if they look ok. See next page. First row is unshifted alphabet, 2nd row is a shift of 1, 3rd row is a shift of 2 and so on.


Cop 3223 c programming spring 2009 arrays in c part 3

The output file “encoded message.dat”


Cop 3223 c programming spring 2009 arrays in c part 3

Output from the decoder program


Cop 3223 c programming spring 2009 arrays in c part 3

Modify the example that begins on page 3 that computes the row and column sums for any matrix so that in addition to row and column sums, it will also produce the sum along the two main diagonals if the array represents a square matrix. If the matrix is not square nothing different happens.

Practice Problems


Cop 3223 c programming spring 2009 arrays in c part 3

Modify the magic square example from pages 8 & 9 so that writes to an output file named “magic square.dat”, the row and column dimensions of the matrix as well as the matrix itself. Then, using this file as the input file to the program you just wrote to solve Practice Problem 1, show that every row, column, and main diagonal sum is the same.

Note that all you should need to do to your solution to Practice Problem 1 is to change the file name that the program reads.

Practice Problems


Cop 3223 c programming spring 2009 arrays in c part 3

Modify the matrix addition example found in Arrays In C – part 2 lecture notes, to multiply two compatible matrices together.

Practice Problems


  • Login