Advertisement
Guest User

Homework assignment 3 - Juli. Class of 2019

a guest
Jul 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /*
  2.     [PROBLEM 3 OVERVIEW]
  3.     Operating System: Linux
  4.     Difficulty: Easy
  5.  
  6.     [PROBLEM 3 DESCRIPTION]
  7.     The code below contains a unique take on the infamous "fizzbuzz" problem.
  8.     You are tasked with modifying the 'system' variable in order to have getFavoriteFood() return a positive number.
  9.     The current implementation below will output a negative number to stdout.
  10.     An approach that involves "guess-and-check" is valid, but try to understand why certain inputs pass and why others fail.
  11.     Feel free to reach out to your peers, tutors, or other online resources if you need additional help.
  12.  
  13.     [PROBLEM 3 FOOD MENU]
  14.     #1: Spaghetti
  15.     #2: Chicken Fettuccine
  16.     #3: Cheeseburger
  17.     #4: Pepperoni Pizza
  18.     #5: Caeser Salad
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. #define SECRET_NUM 29
  25.  
  26. typedef int (*StringToInt)(const char *text);
  27.  
  28. int getFavoriteFood(int *secret, StringToInt atoi)
  29. {
  30.   char *favorite_food = (char *)calloc(SECRET_NUM, sizeof(char));
  31.  
  32.   for (int i = 0; i < SECRET_NUM; i++)
  33.   {
  34.     int counter = secret[i / 4] >> (i % 4 * 8) & 255;
  35.  
  36.     if (i % 3 == 0)
  37.       favorite_food[i] = counter ^ 3;
  38.     else if (i % 5 == 0)
  39.       favorite_food[i] = counter ^ 5;
  40.     else
  41.       favorite_food[i] = counter ^ 15;
  42.   }
  43.  
  44.   return atoi(favorite_food);
  45. }
  46.  
  47. int main()
  48. {
  49.   int _secret[] = {1818717286, 1617895983, 1803363171, 728642596, 1847817254, 690971257, 1647605795, 15};
  50.   int _system = '3';
  51.  
  52.   printf("Problem 3: Favorite Food is %d", getFavoriteFood(_secret, (StringToInt)system));
  53.  
  54.   return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement