Skip to content

Instantly share code, notes, and snippets.

@s3f
Created December 29, 2016 22:57
Show Gist options
  • Save s3f/28ecedabcab4d8376f552543a25b3480 to your computer and use it in GitHub Desktop.
Save s3f/28ecedabcab4d8376f552543a25b3480 to your computer and use it in GitHub Desktop.
Chapter 22- Searching Arrays created by s3f - https://repl.it/EvbH/16
// Searching Arrays
/*
One important use of arrays is filling them with values that you don't have initially and that the user has to enter.
Now the following program below takes an id number from the user and then checks the ID against a list of customers in the database. If the customer exists, it uses that array element to check their current balance, and warns the user if the balance is more than 100.
#include <stdio.h>
main()
{
int ctr; // --> Counter for the loop
int idSearch; // --> customer to look for (the key)
int found = 0; // Will equal 1 (true) if customer has been found
// Defines the 10 elements in the two parrallel arrays
int custID[10] = { 313, 453, 502, 101, 892, 475, 792, 912, 343, 633 };
float custBal[10] = { 0.00, 45.43, 71.23, 301.56, 9.08, 192.41, 389.00, 229.67, 18.31, 59.54 };
// Interact with the user looking for a balance
printf("\n\n*** Customer Balance Lookup ***");
printf("What customer number would you like to check? ");
scanf(" %d", &idSearch);
// Search to see that the customer ID exists in the array
for (ctr = 0; ctr < 10; ctr++)
{
if (idSearch == custID[ctr])
{
found = 1;
break;
}
}
if (found)
{
if (custBal[ctr] > 100.00)
{
printf("\n** That customer's balance is $%.2f.\n", custBal[ctr]);
printf(" No additional credit.\n");
}
else
{
printf("\n** The customer's credit is good!\n");
}
}
else
{
printf("** You must have typed an incorrect customer ID");
printf("\n ID%3d was not found in the list.\n", idSearch);
}
return (0);
}
Now the program below fills 3 arrays with a players total points, rebounds, and assists. It loops through the scoring array and finds the game with the most points. Once it knows that information, it prints the totals from all 3 categories from that game
*/
#include <stdio.h>
main()
{
int gameScores[10] = { 12, 5, 21, 15, 32, 10, 6, 31, 11, 10 };
int gameRebounds[10] = { 5, 7, 1, 5, 10, 3, 0, 7, 6, 4 };
int gameAssists[10] = { 2, 9, 4, 3, 6, 1, 11, 6, 9, 10 };
int bestGame = 0; // This is the comparision variable for best scoring game
int gameMark = 0; // This marks which game is the best scoring game
int i;
for (i = 0; i < 10; i++)
{
/*
The if loop will compare each game to the current best total. If the current score is
higher, it becomes the new best and the counter variable becomes the new flag gameMark.
*/
if (gameScores[i] > bestGame)
{
bestGame = gameScores[i];
gameMark = i;
}
}
printf("\n\nThe player's best scoring game totals: \n");
printf("The best game was #%d\n", gameMark + 1);
printf("Scored %d points\n", gameScores[gameMark]);
printf("Grabbed %d rebounds\n", gameRebounds[gameMark]);
printf("Dished %d assists\n", gameAssists[gameMark]);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment