Professional Documents
Culture Documents
Array Pass
Array Pass
Example:
/* File: frequency1.c
* Mofified by: Aniket Apte
* Modified on: 9/30/02
* SSN: xxx-xx-1234
* Section: 0203
* Email: aniket1@cs.umbc.edu
* Modified for: Discussion 5
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i;
int freq1 = 0;
int freq2 = 0;
int freq3 = 0;
int freq4 = 0;
/*
* Function: PrintResults
* Input: The frequencies to be printed out
* Output: None
* Accepts the frequencies to be printed as a list of
* integer arguments, and prints them out
*/
void PrintResults(int freq1, int freq2, int freq3, int
freq4)
{
/* print out the results */
printf( " Frequency of 1 is %d \n", freq1);
printf( " Frequency of 2 is %d \n", freq2);
printf( " Frequency of 3 is %d \n", freq3);
printf( " Frequency of 4 is %d \n", freq4);
}
[linux1:/afs/umbc.edu/users/a/n/aniket1/home/201] gcc -Wall -ansi
frequency1.c
[linux1:/afs/umbc.edu/users/a/n/aniket1/home/201] ./a.out
Frequency of 1 is 4
Frequency of 2 is 4
Frequency of 3 is 6
Frequency of 4 is 6
Remarks:
Can optionally declare the size of the array
void PrintResults(int freq[4], int size);
Example:
/* File: frequency2.c
* Mofified by: Aniket Apte
* Modified on: 9/30/02
* SSN: xxx-xx-1234
* Section: 0203
* Email: aniket1@cs.umbc.edu
*/
#include <stdio.h>
#include <stdlib.h>
#define NUMBERS 4
int main()
{
int num, i;
int freq[NUMBERS] = {0, 0, 0, 0};
PrintResults(freq, NUMBERS);
ModifyValues(freq, NUMBERS);
printf(After modifying array:\n);
PrintResults(freq, NUMBERS);
/* return no error */
return 0;
}
/*
* Function: PrintResults
* Input: The frequencies to be printed out, and size of
* array
* Output: None
* Accepts the frequencies to be printed as an array, and
* prints them out
*/
void PrintResults(int freq[], int size)
{
int i;
/*
* Function: ModifyValues
* Input: The frequencies to be modified, and size of array
* Output: None
* Accepts the frequencies to be modified as an array, and
* increments each of them by one
*/
void ModifyValues(int freq[], int size)
{
int i;
3-D array
void Print3DArray(int a[][5][5], int x,
int y, int z);
Example:
/*
* File: matrixadd.c
* Author: Aniket Apte
* Date: 9/30/02
* Section: 0203
* SSN: 123-45-6789
* Email: aniket1@cs.umbc.edu
*/
#include <stdio.h>
#define X 3
#define Y 3
int main()
{
int term1[X][Y] = { {0, 1, 4},
{-4, 3, 10},
{0, 0, 4} };
/* calculate sum */
CalcSum(total, term1, term2, X, Y);
/*
* Function: CalcSum
* Input: 3 2-D matrices, and their sizes
* Output: None
* Adds the matrices term1 and term2 and puts results in
* total
*/
void CalcSum(int total[][Y], int term1[][Y],
int term2[][Y], int x, int y)
{
int i, j;
/*
* Function: PrintMatrix
* Input: 2-D matrix and its size
* Output: None
* Prints a 2D matrix.
*/
void PrintMatrix(int term[][Y], int x, int y)
{
int i, j;
Matrix term2:
5 3 -4
14 0 8
0 -10 8
Matrix total:
5 4 0
10 3 18
0 -10 12
Sorting and Searching
Importance of sorting
Fundamental part of many tasks
Required for the efficient performance of other tasks (e.g.
searching a phonebook)
Algorithms
Many algorithms out there with varying degrees of complexity,
speed etc.
Selection Sort
Insertion Sort
Bubble Sort
Analysis of Algorithms
How fast is it?
How much memory does it need?
Selection Sort
Finds the smallest value in the unsorted portion of array
Moves this value to the current position
Insertion Sort
Sorting while playing cards.
Extract a card, shift the remaining cards, and then insert the
extracted card in the correct place.
Repeated until all the cards are in the correct sequence.
Linear Search
Simply go through the array element by element
Binary Search
Works on a sorted array
Compare the middle element of array with the search key
If it matches, the subscript is returned
Otherwise, the problem is reduced to searching one half of
the array
If the key is greater than the middle element, restrict search
to second half array
Otherwise restrict search to first half
UNIX Redirection
Output Redirection
Output of a UNIX command goes to standard output i.e. the
terminal screen E.g.
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] date
Tue Oct 1 01:39:50 EDT 2002
We can redirect (i.e. save) the output into a file by using the >
operator after the command. E.g.
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] date > date.txt
will save the output of the date command into the file
date.txt. If we open the file in an editor, we can see the
output.
The > redirection operator will overwrite into the file
specified.
If we want to append to a file, we use the >> operator. E.g.
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] date > date.txt
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] cat date.txt
Tue Oct 1 01:46:19 EDT 2002
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] date >>
date.txt
[linux3:/afs/umbc.edu/users/a/n/aniket1/home/201] cat date.txt
Tue Oct 1 01:46:19 EDT 2002
Tue Oct 1 01:46:29 EDT 2002
Input Redirection
Input to a UNIX command comes from standard input i.e.
the terminal screen.
We can use the < operator and specify a file that becomes the
standard input to the command.
Example:
/*
* File: stdinput.c
* Author: Aniket Apte
* Date: 9/30/02
* Section: 203
* SSN: 123-45-6789
* Email: aniket1@cs.umbc.edu
*
* Demonstrates redirection of standard input.
*/
#include <stdio.h>
int main()
{
int n1, n2;
printf(Enter n1: );
scanf(%d, &n1);
printf(Enter n2: );
scanf(%d, &n2);
Input from the redirected file does not appear on the screen