Lab 22 Structures - Introduction to Computer Programming | CPS 196, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Fall 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-obp
koofers-user-obp 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPS 196 Lab 22 Fall 2007
Structures
I. Parallel Arrays and Simple Searching
Add this program to Visual Studio either from this page or by using the links in the
browser lecture page.
/* coinarray.c */
/* This program converts the name of a U.S. coin to the number of cents
that it is worth, and vice versa. */
/* It uses two parallel arrays to store the coin names and values. */
#include <stdio.h>
#include <string.h>
#define LENGTH 12 // coin names can be up to 11 characters
/* simple search functions for integer arrays and string arrays */
int findIntInArray(int key, int elements[ ], int numberOfElements);
int findStringInArray(char key[ ], char elements[ ][LENGTH], int
numberOfElements);
int main (void)
{
/* parallel arrays for coin values and names */
int coinValues[] = { 1, 5, 10, 25, 50 };
char coinNames[][LENGTH] =
{"penny", "nickel", "dime", "quarter", "half-dollar"};
int value, index;
char name[LENGTH];
printf("This program can convert a coin value to its name.\n");
printf("Enter coin value: ");
scanf("%i", &value);
index = findIntInArray(value, coinValues, 5);
/* test if coin value not found */
if (index == -1)
{
printf("There is no such coin.\n\n");
}
else
{
printf("That's called a %s.\n\n", coinNames[index]);
}
printf("This program will also convert a coin name to its value.\n");
printf("Enter coin name: ");
scanf("%s", name);
index = findStringInArray(name, coinNames, 5);
/* test if coin name not found */
if (index == -1)
{
printf("There is no such coin name.\n\n");
}
pf3
pf4
pf5

Partial preview of the text

Download Lab 22 Structures - Introduction to Computer Programming | CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!

CPS 196 Lab 22 Fall 2007

Structures

I. Parallel Arrays and Simple Searching

Add this program to Visual Studio either from this page or by using the links in the

browser lecture page.

/* coinarray.c / / This program converts the name of a U.S. coin to the number of cents that it is worth, and vice versa. / / It uses two parallel arrays to store the coin names and values. */ #include <stdio.h> #include <string.h> #define LENGTH 12 // coin names can be up to 11 characters

/* simple search functions for integer arrays and string arrays */ int findIntInArray(int key, int elements[ ], int numberOfElements); int findStringInArray(char key[ ], char elements[ ][LENGTH], int numberOfElements);

int main (void) { /* parallel arrays for coin values and names */ int coinValues[] = { 1, 5, 10, 25, 50 }; char coinNames[][LENGTH] = {"penny", "nickel", "dime", "quarter", "half-dollar"};

int value, index; char name[LENGTH];

printf("This program can convert a coin value to its name.\n"); printf("Enter coin value: "); scanf("%i", &value);

index = findIntInArray(value, coinValues, 5); /* test if coin value not found */ if (index == -1) { printf("There is no such coin.\n\n"); } else { printf("That's called a %s.\n\n", coinNames[index]); }

printf("This program will also convert a coin name to its value.\n"); printf("Enter coin name: "); scanf("%s", name);

index = findStringInArray(name, coinNames, 5); /* test if coin name not found */ if (index == -1) { printf("There is no such coin name.\n\n"); }

else { printf("That's worth %i cent(s).\n\n", coinValues[index]); } }

/* This function searches for the integer key in the array. If it is found in the array, return the array index number where it occurs. If it is not found in the array, return -1. / int findIntInArray(int key, int elements[ ], int numberOfElements) { int i; / loop through array searching for the key value / for (i = 0; i < numberOfElements; i++) { / if the key value is found, return from the function with the array index / if (key == elements[i]) { return i; } } / if the loop completes, the key value was not found, return -1 */ return -1; }

/* This function searches for the key string in the array. If it is found in the array, return the array index number where it occurs. If it is not found in the array, return -1. / int findStringInArray(char key[ ], char elements[ ][LENGTH], int numberOfElements) { int i; / loop through array searching for the key value / for (i = 0; i < numberOfElements; i++) { / if the key value is found, return from the function with the array index / if (strcmp(key, elements[i]) == 0) { return i; } } / if the loop completes, the key value was not found, return -1 */ return -1; }

II. Structures and table lookup.

Add this program to Visual Studio either from this page or by using the links in the

browser lecture page. (Note that the lookup functions are another example of simple

search functions.)

/* coinstructure.c / / This program converts the name of a U.S. coin to the number of cents that it is worth, and vice versa. / / It uses an array of coin structures to store the coin names and values. */ #include <stdio.h> #include <string.h> #define LENGTH 12

struct coin { int coinvalue; char coinname[LENGTH]; };

/* these lookup functions search the array for either the value or the name of the coin */ int lookupInt(int key, struct coin elements[ ], int numberOfElements); int lookupString(char key[ ], struct coin elements[ ], int numberOfElements);

int main (void) { /* array of structures for coin values and names */ struct coin coins[] = { { 1, "penny"}, { 5, "nickel"}, {10, "dime" }, {25, "quarter"}, {50, "half-dollar"} };

int value, index; char name[LENGTH];

printf("This program can convert a coin value to its name.\n"); printf("Enter coin value: "); scanf("%i", &value);

index = lookupInt(value, coins, 5); /* test if coin value not found */ if (index == -1) { printf("There is no such coin.\n\n"); } else { printf("That's called a %s.\n\n", coins[index].coinname); }

printf("This program will also convert a coin name to its value.\n"); printf("Enter coin name: "); scanf("%s", name);

index = lookupString(name, coins, 5); /* test if coin name not found */ if (index == -1) { printf("There is no such coin name.\n\n"); } else { printf("That's worth %i cent(s).\n\n", coins[index].coinvalue); } }

/* This function searches for the integer key in the coinvalue position of the array. If it is present in the array, return the array index number. If it is not found in the array, return -1. / int lookupInt(int key, struct coin elements[ ], int numberOfElements) { int i; / loop through array searching for the key in the coinvalue position / for (i = 0; i < numberOfElements; i++) { / if the key value is found, return from the function with the array index / if (key == elements[i].coinvalue) { return i; } } / if the loop completes, the key value was not found, return -1 */ return -1; }

/* This function searches for the key string in the coinname position of the array. If it is present in the array, return the array index number. If it is not found in the array, return -1. / int lookupString(char key[], struct coin elements[], int numberOfElements) { int i; / loop through array searching for the key value / for (i = 0; i < numberOfElements; i++) { / if the key value is found, return from the function with the array index / if (strcmp(key, elements[i].coinname) == 0) { return i; } } / if the loop completes, the key value was not found, return -1 */ return -1; }