Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSC 373 Computer Systems 1 Sections 401, 410 Fall 2018 Homework assignment 1 Due

ID: 3753186 • Letter: C

Question

CSC 373 Computer Systems 1 Sections 401, 410 Fall 2018

Homework assignment 1

Due as specified on D2L

Each problem below is worth 2 points, for a total of 10 points. Please see the file hw1_main.c for sample calls to each function. Note that I may test your code on other examples, so you should thoroughly test the functions that you write, beyond those provided in hw1_main.

Write 2 functions, called scan_2 and print_2. Here are prototypes for each:

void scan_2(int *i, char *c);

void print_2(int i, char c);

Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below.

int pow_xy(int *xptr, int y);

Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result.

Write a function called convert_temp. The prototype for this function is:

int convert_temp(int deg, char scale, int *dptr, char *sptr);

The function should convert a temperature from Fahrenheit to Celsius or vice versa, depending on the value of the parameter scale.   The function should return an int reflecting whether or not the converted value *dptr is positive.

Write a function called init_array. It is passed 3 parameters, as specified in the prototype below:

int init_array(int array[ ], int len, int start_value);

The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.

Write a function called reverse_numbers. The function is passed an array of ints (and of course its length as a 2nd parameter). The function should reverse the ordering of the numbers in the array.   Here is its prototype:

void reverse_numbers(int nums[], int len);

Explanation / Answer

#include <stdio.h>

//Function to accept a integer an character
void scan_2(int *i, char *c)
{
scanf("%d%c", i, c);
}// End of function

// Function to display the values
void print_2(int i, char c)
{
printf(" Integer: %d Character = %c", i, c);
}// End of function

// Function to calculate x to the power y
int pow_xy(int *xptr, int y)
{
// To store the calculated power
int power = 1;
int x;
// Loops till power value (y)
for(x = 1; x <= y; x++)
// Multiplies with base value
power = power * *xptr;
// Returns the calculated power
return power;
}// End of function

// Function to convert degree in Fahrenheit to Celsius and vice versa
int convert_temp(int degree, char scale, int *other_degree, char *other_scale)
{
// Checks if scale contents is 'F' then convert it into Celsius
if(scale == 'F')
{
(*other_degree - 32) * .5556;
// Sets the other scale to 'C' for Celsius
*other_scale = 'C';
}// End of if condition
// Otherwise checks if scale contents is 'C' then convert it into Fahrenheit
else if('C')
{
(*other_degree * 1.8) + 32;
// Sets the other scale to 'F' for Fahrenheit
*other_scale = 'F';
}// End of else if condition
// Otherwise invalid scale
else
printf(" Invalid choice!");

// Checks if the calculated degree is negative return -1
if(*other_degree < 0)
return -1;
// Otherwise return 1
else
return 1;
}// End of function

// Function to accept numbers to an array and displays the sum
int init_array(int array[ ], int len, int start_value)
{
// To store sum
int sum = 0;
int i;
// Loops till length of the array
for(i = 0; i < len; i++)
{
// Multiplies loop value with start_value and stores it at ith index position of the array
array[i] = start_value * i;
// Calculates sum of each element of the array
sum += array[i];
}// End of for loop
// Returns sum
return sum;
}// End of function

// Function to display the reverse of an array
void reverse_numbers(int nums[], int len)
{
// Loops variable
int i;
// Variable to swap
int temp;
// Variable to point to last index position of the array
int end = len-1;

// Loops till half of the length of the array
for(i = 0; i < len/2; i++, end--)
{
// Swaps first to last position of the array
temp = nums[i];
nums[i] = nums[end];
nums[end] = temp;
}// End of for loop
}// End of function

--------------------------------------------------------------------------------------------------

#include <stdio.h>
// includes your hw1.c file (not from C library)
// note " " instead of < >
#include "hw1.c"

// Function to accept a integer an character
void test_p1()
{
printf(" p1 ");
int integer;
char character;
printf("Type an int and a char (no blanks) ");
// Calls the function to accept a number and a character
scan_2(&integer, &character);
// Calls the function to display a number and a character
print_2(integer, character);
}// End of function

// Function to calculate x to the power y
void test_p2()
{
printf(" p2 ");
// Assigns base value
int x = 2;
// Assigns the address of x
int *xptr = &x;
printf("pow_xy %d 3 ", x);
// Calls the function to calculate the power
int power = pow_xy(xptr, 3);
// Displays the power
printf("= %d ", power);
}// End of function

// Function to convert degree in Fahrenheit to Celsius and vice versa
void test_p3()
{
printf(" p3 ");
int new_degrees;
char new_scale;

// Calls the function to convert Fahrenheit to Celsius
convert_temp(32, 'F', &new_degrees, &new_scale);
// Displays the result
printf(" 32 F is %d %c ", new_degrees, new_scale);

// Checks if the new degree is negative
if(new_degrees == -1)
printf(" New degree is negative");
// Otherwise positive
else
printf(" New degree is positive");

// Calls the function to convert Celsius to Fahrenheit
convert_temp(10, 'C', &new_degrees, &new_scale);
// Displays the result
printf(" 10 C is %d %c ", new_degrees, new_scale);

// Checks if the new degree is negative
if(new_degrees == -1)
printf(" New degree is negative");
// Otherwise positive
else
printf(" New degree is positive");
}// End of function

// Function to accept numbers to an array and displays the sum
void test_p4()
{
printf(" p4 ");
// Declares an array of size 5
int numbers[5];
// Calls the function to assign values to the array
// and stores the return sum
int sum = init_array(numbers, 5, 2);
int i;
// Loops 5 times and displays array contents
for (i = 0; i < 5; i++)
printf("%d ", numbers[i]);
// Displays the sum
printf(" Sum of the array = %d", sum);
printf(" ");
}// End of function

// Function to display the reverse of an array
void test_p5()
{
printf(" p5 ");
int i;
// Creates an array
int phone[] = {3, 1, 2, 3, 6, 2, 6, 1, 0, 6};

// Displays the original array contents
printf(" Original order: ");
// Loops 10 times and display array contents
for (i = 0; i < 10; i++)
printf("%d ", phone[i]);

// Calls the function to reverse the contents of the array
reverse_numbers(phone, 10);

// Displays the reverse array contents
printf(" Reverse order: ");
// Loops 10 times and display array contents
for (i=0; i<10; i++)
printf("%d ", phone[i]);
printf(" ");
}// End of function

// main function definition
int main()
{
// Calls the function to test
test_p1();
test_p2();
test_p3();
test_p4();
test_p5();
}// End of function

Sample Output:

p1
Type an int and a char (no blanks)
15D

Integer: 15
Character = D
p2
pow_xy 2 3 = 8

p3

32 F is 6356724 C

New degree is positive
10 C is 6356724 F

New degree is positive
p4
0 2 4 6 8
Sum of the array = 20

p5

Original order: 3 1 2 3 6 2 6 1 0 6
Reverse order: 6 0 1 6 2 6 3 2 1 3