Debugging for c program: part1_functions.c (please modify) part1_functions.h (do
ID: 3812925 • Letter: D
Question
Debugging for c program:
part1_functions.c (please modify)
part1_functions.h (do not modify)
part1_main.c (do not modify)
recitation1.txt (do not modify)
The 'functions' files are a header file containing function prototypes, and a source file containing implementations. The 'main' file has a series of calls to code defined in the 'functions' files. Do not alter part1_main.c or part1_functions.h. recitation1.txt has text representing what the 'main' function should output when compiled and run. The problem is much of the code in part1_functions.c is incorrect.
Your assignment is to:
Look at how the functions are defined in part1_functions.h
Look at how the functions are called in part1_main.c
Look at what the function output ought to be in recitation1.txt
Look at the comments in part1_main.c to see what the function ought to do
Compile and run the functions to see what they do/what happens
Fix the function implementations in part1_functions.c so they run as expected and their output
is the same as the output in recitation1.txt
******************************************************************************
part1_functions.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Calculates a value (c) based on the input parameters (a, b) and prints
* out the result.
*
* @param a
* Input parameter a.
*
* @param b
* Input parameter b.
*/
void one(const int a, const int b) {
int c = (a * a) + (b * b);
printf("%d^2 + %d^2 = %d", a, b, c);
}
/**
* Checks if the input parameter is a passing grade and prints out if
* the grade is passing.
*
* @param grade
* The grade to check.
*/
void two(const char *grade) {
// you may find the atoi function useful
if (grade > 70)
printf("%d passed! ", grade);
else
printf("%d not passed! ", grade);
}
/**
* Assigns a pointer (int *p) the value of a stack variable (int x).
*/
void three() {
int x = 4;
int *p = x;
printf("The value of p is: %d ", *p);
}
/**
* Prints out a specific message based on if the number is
* between 0 and 1 (exclusive).
*
* @param value
* Value to test.
*/
void four(const float value) {
if (0 < value < 1)
printf("The value is between zero and one. ");
else
printf("The value is not between zero and one. ");
}
/**
* Prints out a specific message based on if the two input parameters
* are equal.
*
* @param x
* First input parameter.
*
* @param y
* Second input parameter.
*/
void five(const int *x, const int *y) {
if (x == y)
printf("x and y are equal. ");
else
printf("x and y are different. ");
}
/**
* Returns a new pointer (float *p) which contains the value of the
* input pointer (int *x).
*
* @param x
* Input parameter, whose value will be returned as a (float *).
*
* @returns
* A new pointer, allocated on the heap and not freed, that
* contains the value of the input parameter.
*/
float *six(const int *x) {
float *p = *x;
return p;
}
/**
* Takes an input a and checks whether a is an alphabet, it can be
* both uppercase and lowercase
*
* @param a
* Input parameter a, which is a char*
*
*/
void seven(const char *a) {
if (a >= 'A' && a <= 'z')
printf("a is a letter. ");
else
printf("a is not a letter. ");
}
/**
* Constructs a C-string, character by character, and prints out the full
* string "Hello".
*/
void eight() {
char *s = malloc(5);
s[0] = 'H';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
s[5] = '';
printf("%s ", s);
free(s);
}
/**
* Assigns a pointer (float *p) a numeric value (12.5).
*/
void nine() {
float *p;
p = 12.5;
printf("The value of p is: %f ", *p);
}
/**
* Reset the value of x to zero.
*
* @param x
* Pointer to reset to 0.
*/
void ten(int *x) { x = 0; }
/**
* Concatenates "Hello " and the parameter str, which is guaranteed to be a
* valid c string, and
* prints the concatenated string.
*/
void eleven(const char *str) {
char *s = "Hello ";
strcat(s, str);
printf("%s ", s);
}
/**
* Creates an array of values containing the values {0.0, 0.1, ..., 0.9}.
*/
void twelve() {
float *values;
int i, n = 10;
for (i = 0; i < n; i++)
values[i] = (float)i / n;
for (i = 0; i < n; i++)
printf("%f ", values[i]);
printf(" ");
}
/**
* Creates a 2D array of values and prints out the values on the diagonal.
*/
void thirteen(int a) {
int **values;
int i, j;
values = malloc(10 * sizeof(int));
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
values[i][j] = i * j * a;
for (i = 0; i < 10; i++)
printf("%d ", values[i][i]);
printf(" ");
}
/**
* Prints out a specific string based on the input parameter (s).
*
* @param s
* Input parameter, used to determine which string is printed.
*/
void fourteen(const char *s) {
switch (s) {
case "blue":
printf("Orange and BLUE! ");
break;
case "orange":
printf("ORANGE and blue! ");
break;
default:
printf("orange and blue! ");
break;
}
}
/**
* Prints out a specific string based on the input parameter (value).
*
* @param value
* Input parameter, used to determine which string is printed.
*/
void fifteen(const int value) {
switch (value) {
case 1:
printf("You passed in the value of one! ");
case 2:
printf("You passed in the value of two! ");
default:
printf("You passed in some other value! ");
}
}
/**
* Returns a newly allocated string on the heap with the value of "Hello".
* This should not be freed.
*
* @returns
* A newly allocated string, stored on the heap, with the value "Hello".
*/
char *sixteen() {
char *s = malloc(5);
strcpy(s, "Hello");
}
/**
* Prints out the radius of a circle, given its diameter.
*
* @param d
* The diameter of the circle.
*/
void seventeen(const int d) {
printf("The radius of the circle is: %f. ", d / 2);
}
/**
* Manipulates the input parameter (k) and prints the result.
*
* @param k
* The input parameter to manipulate.
*/
void eighteen(const int k) {
k = k * k;
k += k;
k *= k;
k -= 1;
printf("Result: %d ", k);
}
/**
* @brief
* Clears the bits in "value" that are set in "flag".
*
* This function will apply the following rules to the number stored
* in the input parameter "value":
* (1): If a specific bit is set in BOTH "value" and "flag", that
* bit is NOT SET in the result.
* (2): If a specific bit is set ONLY in "value", that bit IS SET
* in the result.
* (3): All other bits are NOT SET in the result.
*
* Examples:
* clear_bits(value = 0xFF, flag = 0x55): 0xAA
* clear_bits(value = 0x00, flag = 0xF0): 0x00
* clear_bits(value = 0xAB, flag = 0x00): 0xAB
*
* @param value
* The numeric value to manipulate.
*
* @param flag
* The flag (or mask) used in order to clear bits from "value".
*/
long int clear_bits(long int value, long int flag) {
// TODO clear_bits
}
******************************************************************************
part1_functions.h
#ifndef __PART_1_FUNCTIONS_H__
#define __PART_1_FUNCTIONS_H__
void one(const int a, const int b);
void two(const char *grade);
void three();
void four(const float value);
void five(const int *x, const int *y);
float *six(const int *x);
void seven(const char *a);
void eight();
void nine();
void ten(int *x);
void eleven(const char *s);
void twelve();
void thirteen(int a);
void fourteen(const char *s);
void fifteen(const int value);
char *sixteen();
void seventeen(const int d);
void eighteen(int k);
long int clear_bits(long int value, long int flag);
#endif // __PART_1_FUNCTIONS_H__
******************************************************************************
part1_main.c
#include <stdio.h>
#include <stdlib.h>
#include "part1_functions.h"
int main() {
printf("== one() == ");
one(3, 4);
one(10, 10);
printf("== two() == ");
const char *a = "20";
two(a);
const char *b = "100";
two(b);
printf("== three() == ");
three();
printf("== four() == ");
four(0.5);
four(1.5);
printf("== five() == ");
const int num1 = 3;
const int num2 = 3;
five(&num1, &num2);
const int num3 = 4;
five(&num1, &num3);
printf("== six() == ");
float *p_six;
int i4 = 4, i432 = 432;
p_six = six(&i4);
printf("%d == %f ", i4, *p_six);
free(p_six);
p_six = six(&i432);
printf("%d == %f ", i432, *p_six);
free(p_six);
printf("== seven() == ");
const char s = 'S';
seven(&s);
const char t = '_';
seven(&t);
printf("== eight() == ");
eight();
printf("== nine() == ");
nine();
printf("== ten() == ");
int i_ten = 100;
ten(&i_ten);
printf("%d == 0? ", i_ten);
printf("== eleven() == ");
eleven("World!");
printf("== twelve() == ");
twelve();
printf("== thirteen() == ");
thirteen(10);
printf("== fourteen() == ");
fourteen("red");
fourteen("orange");
fourteen("blue");
fourteen("green");
printf("== fifteen() == ");
fifteen(1);
fifteen(2);
fifteen(3);
printf("== sixteen() == ");
char *str = sixteen();
printf("%s ", str);
free(str);
printf("== seventeen() == ");
seventeen(35);
seventeen(20);
printf("== eighteen() == ");
eighteen(3);
eighteen(5);
printf("== clear_bits() == ");
long int result;
result = clear_bits(0xFF, 0x55);
printf("%ld ", result);
result = clear_bits(0x00, 0xF0);
printf("%ld ", result);
result = clear_bits(0xAB, 0x00);
printf("%ld ", result);
result = clear_bits(0xCA, 0xFE);
printf("%ld ", result);
result = clear_bits(0x14, 0x00);
printf("%ld ", result);
result = clear_bits(0xBB, 0xBB);
printf("%ld ", result);
return 0;
}
******************************************************************************
recitation1.txt
Explanation / Answer
part1_function.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void one(const int a, const int b)
{
int c = (a * a) + (b * b);
printf("%d^2 + %d^2 = %d", a, b, c);
}
void two(const char *grade) {
if(grade == NULL)
return;
//atof() is used to convert string to floating point value and also handles the integer values too
float num = atof(grade);
if (num > 70)
printf("%f passed! ", num);
else
printf("%f not passed! ", num);
}
void three() {
int x = 4;
int *p = &x;
printf("The value of p is: %d ", *p);
}
void four(const float value) {
if( (0 < value) && (value < 1))
printf("The value is between zero and one. ");
else
printf("The value is not between zero and one. ");
}
/*I believe we are comapring the values pointed by the pointers 'x' & 'y'*/
void five(const int *x, const int *y) {
if (*x == *y)
printf("x and y are equal. ");
else
printf("x and y are different. ");
}
float *six(const int *x) {
float *p = (float*)malloc(sizeof(float));
*p = *x;
return p;
}
void seven(const char *a) {
if((*a >= 'A' && *a <= 'Z') || (*a >= 'a' && *a <= 'z'))
printf("a is a letter. ");
else
printf("a is not a letter. ");
}
void eight() {
char *s = (char*)malloc(6 * sizeof(char));
s[0] = 'H';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
s[5] = '';
printf("%s ", s);
free(s);
}
void nine() {
float val = 12.5;
float *p = &val;
printf("The value of p is: %f ", *p);
}
void ten(int *x)
{
*x = 0;
}
void eleven(const char *str) {
size_t len = strlen(str) + sizeof("Hello ");
char *s = (char*)malloc(len);
strncpy(s,"Hello ",6);
strcat(s, str);
printf("%s ", s);
free(s);
}
void twelve() {
float values[10] = {0.0};
int i, n = 10;
for (i = 0; i < n; i++)
values[i] = ((float)i) / n;
for (i = 0; i < n; i++)
printf("%f ", values[i]);
printf(" ");
}
void thirteen(int a) {
int **values;
int i, j;
values = (int**)malloc(10 * sizeof(int*));
for (i = 0; i < 10; i++){
values[i] = (int*)malloc(10 * sizeof(int));
for (j = 0; j < 10; j++){
values[i][j] = i * j * a;
}
}
for (i = 0; i < 10; i++){
printf("%d ", values[i][i]);
}
printf(" ");
for(i=0; i< 10;i++)
free(values[i]);
free(values);
}
void fourteen(const char *s) {
if(s == NULL)
return;
if(!strncmp(s,"blue",4)){
printf("Orange and BLUE! ");
}
else if(!strncmp(s,"orange",6)){
printf("ORANGE and blue! ");
}
else{
printf("orange and blue! ");
}
}
void fifteen(const int value) {
switch (value) {
case 1:
printf("You passed in the value of one! ");
break;
case 2:
printf("You passed in the value of two! ");
break;
default:
printf("You passed in some other value! ");
break;
}
}
char *sixteen() {
char *s = (char*)malloc(6 * sizeof(char));
strcpy(s, "Hello");
s[5]='';
return s;
}
void seventeen(const int d) {
printf("The radius of the circle is: %f. ", d / 2.0);
}
void eighteen(const int k) {
int val = k;
val = val * val;
val += val;
val *= val;
val -= 1;
printf("Result: %d ", val);
}
long int clear_bits(long int value, long int flag) {
return ( value & (~flag));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.