Problem1/ fixing bugs The programs b001.c - b010.c are provided for you in the a
ID: 3562437 • Letter: P
Question
Problem1/ fixing bugs
The programs b001.c - b010.c are provided for you in the attached file "bugs.zip". Each program has a bug which needs to be found and eliminated. The correct output from each program appears in the file "outputs_from_corrected_b001-b010.txt". You are to:
i) place each source file into a separate directories "ENEE150HW3/Problem1/001/", "ENEE150HW2/Problem1/002/", etc ;
ii) create a debug startup file ".gdbinit"in each directory which will (at least) "set history save on" so that a record of the steps you took to debug the program will be preserved in a ".gdb_history" file in the directory;
iii) debug each program with gdb, find the bug, and comment your fixups with a "//!! " . Problem2/ limits of data types part_a/ finding limits with a program -- limits.c limitfns.c limitfns.h Makefile in /part_a/ Program 'limits' calls functions in limitsfns.c which determine by searching in a 'reasonable' amount of time for the real value limits (highest/greatest/largest,lowest/least/smallest and not 'nan', 'infinity', +0 or -0) of several C datatypes. The functions (names indicate target datatype): i) char greatest_char(void); ii) char least_char(void); iii) unsigned char greatest_unsigned_char(void); iv) long int greatest_long_int(void); v) long double greatest_long_double(void); vi) long_double smallest_positive_non-zero_long_double(void); part_b/ exploring limits with the debugger -- debuglimits.c in
/part_b/ Write a small program 'debuglimits.c' which defines and then prints the values of a char, unsigned char, long int, and long double (the types used for Problem2 above). Create a debug startup file ".gdbinit"which will (at least) "set history save on" so that a record of the steps you took during program debug will be preserved in a ".gdb_history" file in the /part_b/ directory. Run your program under gdb and modify the values stored in memory 'by hand' until you discover the greatest, least, or smallest values of variables as per Problem2 above, then let the program print out those values.
files included:
b001.c
#include <stdio.h>
  #define LENGTH 10
  char a[LENGTH] = "567891234";
  int ith_character( int );
 Â
  int main()
  {
  int len = sizeof(a[LENGTH]);
  printf(
  "It's time to celebrate"
  " the %cth of July ",
  ith_character( len-2 ) );
  return 0;
  }
 Â
  int ith_character( int n )
  {
  return a[n];
  }
b002.c  Â
#include <stdio.h>
 Â
  typedef const char *S;
  typedef char Player[100];
 Â
  void asgn_position( Player name, S pos )
  {
  sprintf( name, "%s %s", pos, name );
  }
 Â
  int main()
  {
  Player player = "Joe Jackson";
  asgn_position( player, "QB" );
  printf( "%s ", player );
  return 0;
  }
b003.c
#include <stdio.h>
  #define Hot_Dog 1
  #define Potato_Salad 2
  #define Green_Salad 4
  #define Hummus_Sandwich 8
  unsigned Grand_Menu = 0x70E;
  unsigned select(int v) // v means vegetarian
  {
  return (Grand_Menu >> !v ? 8 : 0) & 0xFF;
  }
  int main()
  {
  printf( "%x ", select(0) );
  }
b004.c
#include <stdio.h>
  #include <string.h>
 Â
  char April[] = "April";
  void swap( char *p, char *q )
  { char t = *p; *p = *q; *q = t; }
  void reverse( char *s, size_t len )
  {
  char *top, *bot;
  for( bot = s, top = s+len-1;
bot < top, *bot;
bot++, top-- )
  swap( bot, top );
  }
  int main()
  {
  reverse( April, strlen(April) );
  printf( "%s ", April );
  return 0;
  }
b005.c
#include <stdio.h>
  #define DOCTORATE 1
  #define CHOICE 2
  const char *a[2][3] =
  {
  {"Hitler", "Stalin", "Bin Laden"},
  {"Clinton", "Bush", "Powell" }
  };
  int main()
  {
  const char **array;
  #if D0CT0RATE
  array = a[1];
  #else
  array = a[0];
  #endif
  printf( "An honorary doctorate award "
  " is issued to %s ",
  array[CHOICE] );
  }
b006.c
#include <stdio.h>
  const char *bouquet = "DRMÿ";
  const char *flower( char ch )
  {
  switch( (unsigned) ch )
  {
  case 'D': return "Daisies";
  case 'R': return "Roses";
  case 'M': return "Mums";
  case 0xFF: return "Baby's Breath";
  default: return "What?";
  }
  }
  int main()
  {
  const char *b, *flwr;
  printf( "Bridal bouquet: " );
  for( b = bouquet; *b; b++ )
  {
  flwr = flower( *b );
  if(flwr) printf( "%s ",flwr);
  else break;
  }
  return 0;
  }
  b007.c
#include <stdio.h>
 Â
  const int march[31] = {
  8, 5, 7, 2, -4, -14, -7, -4, -2, 0,
  0, 2, 5, 7, 2, -4, -14, -7, -4, -2,
  1, 7, 2, 2, -2, -3, -4, 6, -4, 3, 9 };
 Â
  int main()
  {
  unsigned i, count = 31;
  int sum = 0;
 Â
  for( i = 0; i < count; i++ )
  {
  sum += march[ i ];
  }
  printf( "The average low temperature in March was"
  " %d degrees ", sum / count );
  return 0;
  }
b008.c
#include <stdio.h>
 Â
  const int march[31] = {
  8, 5, 7, 2, -4, -14, -7, -4, -2, 0,
  0, 2, 5, 7, 2, -4, -14, -7, -4, -2,
  1, 7, 2, 2, -2, -3, -4, 6, -4, 3, 9 };
 Â
  int main()
  {
  unsigned i, count = 31;
  int sum = 0;
 Â
  for( i = 0; i < count; i++ )
  {
  sum += march[ i ];
  }
  printf( "The average low temperature in March was"
  " %d degrees ", sum / count );
  return 0;
  }
  b009.c
#include <stdio.h>
 Â
  const char *wind[4] =
  { "N 12 MPH", "NW 14 MPH", "N 22 MPH", NULL };
 Â
  float average( const char *a[] )
  {
  float sum=0, vel=0;
  int i;
  char direction[10];
  for( i = 0; a[i]; i++ ) {
  sscanf( a[i], "%s %d", direction, &vel );
  sum += vel;
  }
  return sum / i;
  }
 Â
  int main()
  {
  printf( "Average wind velocity is %g. ", average(wind) );
  return 0;
  }
  b010.c
#include <stdio.h>
  #include <ctype.h>
  int arithmetic( const char *s )
  {
  int n = 0;
  unsigned char ch;
  switch( *s )
  {
  case '+':
  while( (ch = *++s) )
  {
  if( !isdigit(ch) ) break;
  n = n * 10 + (ch - '0');
  if( !*s ) default:;
  }
  break;
  }
  return n;
  }
  int main()
{ printf( "%d ", arithmetic("-35") ); }
outputs_from_corrected_b001-b010.txt
bash-3.2$ ./1a.exe
It's time to celebrate the 4th of July
bash-3.2$ ./2a.exe
QB Joe Jackson
bash-3.2$ ./3a.exe
7
bash-3.2$ ./4a.exe
lirpA
bash-3.2$ ./5a.exe
An honorary doctorate award is issued to Powell
bash-3.2$ ./6a.exe
Bridal bouquet:
Daisies
Roses
Mums
Baby's Breath
bash-3.2$ ./7a.exe
The average low temperature in March was 0 degrees
bash-3.2$ ./8a.exe
Top 3 vote percentages were:
johnson 61.1% f. roosevelt 60.8% nixon 60.7% bash-3.2$
bash-3.2$ ./9a.exe
Average wind velocity is 16.
bash-3.2$ ./10a.exe
-35
 Â
Explanation / Answer
/* C program to check either prime number or Armstrong number depending upon the data entered by user. */ #include int prime(int n); int armstrong(int n); int main() { char c; int n,temp=0; printf("Eneter a positive integer: "); scanf("%d",&n); printf("Enter P to check prime and A to check Armstrong number: "); c=getche(); if (c=='p' || c=='P') { temp=prime(n); if(temp==1) printf(" %d is a prime number.", n); else printf(" %d is not a prime number.", n); } if (c=='a' || c=='A') { temp=armstrong(n); if(temp==1) printf(" %d is an Armstrong number.", n); else printf(" %d is not an Armstrong number.",n); } return 0; } int prime(int n) { int i, flag=1; for(i=2; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.