# [Solved] CH-230-A-Assignment 3 Loops, Arrays, Functions and Strings

30 \$

SKU: [Solved] CH-230-A-Assignment 3 Loops, Arrays, Functions and Strings Category: Tag:

# Problem 3.1 Writing numbers

Write a program where you first enter a float x and then an integer n from the keyboard. Print the float xn times to the screen. Make sure that n will have a valid integer value. In the invalid case repeat entering n until a valid value will be entered.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 3.1: input1.25-804 Testcase 3.1: outputInput is invalid, reenter valueInput is invalid, reenter value1.2500001.2500001.2500001.250000 Problem 3.2 Writing characters

## Language: C

Write a program where you first enter a lowercase character ch and then an integer n from the keyboard. Print the characters ch, ch−1, …, ch−n on the screen.

You can safely assume that the input is valid and you do not have to do any checks.

# Problem 3.3 Centimeters to kilometers

Write a program that converts an integer length that is entered from the keyboard in cm to km.

Write and use a function float convert(int cm) that does the actual conversion.

You can safely assume that the input will be valid.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

# Testcase 3.3: inputTestcase 3.3: output

12Result of conversion: 0.000120

## Problem 3.4 Wrong position

The program below should print the position of the first occurrence of the character ’g’ within a

string. You can safely assume that ’g’ will be contained in the string. Why does it always print the position 0? Fix the program such that it prints the correct position.

#include <stdio.h>

int position(char str[], char c)

{ int idx;

/* Loop until end of string */ for (idx = 0; str[idx] != c && str[idx] != ’’; ++idx)

/* do nothing */ return idx;

}

int main() { char line[80]; while (1) { printf(“Enter string:
”); fgets(line, sizeof(line), stdin); printf(“The first occurrence of ’g’ is: %d
”, position(line, ’g’));

}

}

## Problem 3.5 Computing the average of temperature values

Write a program where you first enter a character c followed by an integer n and n double values representing temperatures in Celsius. Use an array for storing the temperatures. You can assume that not more than 100 temperature values would be entered. Your program should compute and/or print the following: if c is ’s’ the sum of the temperatures, if c is ’p’ the list of all temperatures, if c it ’t’ the list of all temperatures in Fahrenheit and if another character was entered then the arithmetic mean (or average) of all temperatures. The formula for converting Celsius to Fahrenheit is the following:

.

Use a switch instruction in your solution.

You can safely assume that the input will be valid.

## Problem 3.6 Kilograms and grams to pounds

Write a program that converts the units of mass kg and g to pounds. First read the weight of an object expressed by values for kilograms and grams from the keyboard and convert the units of mass using the function (written by you as well)

float to_pounds(int kg, int g); that does the actual conversion. Note that: 1 kilogram =2.2 pounds

You can safely assume that the input will be valid.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

# Testcase 3.6: inputTestcase 3.6: output

5Result of conversion: 11.220000

100

## Problem 3.7 Printing a form

Due by Monday, September 27th, 23:00

### Language: C

Write a program which reads two integers n, m and a character c from the keyboard. This program should define and call a function with the prototype:

void printform(int n, int m, char c);

which prints a trapezoid of height n, bases m and m+n-1 consisting of the character c as in the following testcase.

 Testcase 3.7: input43\$ Testcase 3.7: output\$\$\$\$\$\$\$\$\$\$\$\$

\$\$\$\$\$\$

You can safely assume that the input will be valid.

## Problem 3.8 Computing sum and average

### Language: C

Write a program where you can enter from the keyboard up to 10 floats. If the number entered is equal to −99.0, stop reading numbers from the keyboard and compute the sum and average of all values (excluding −99.0) using two functions (one for the sum and one for the average). Print your results on the screen.

You can safely assume that the input will be valid.

Make sure you consider all the cases: less than 10 numbers might be entered. After all the numbers have been entered you need to make sure that the sum and average are computed.

## Problem 3.9 Determine sum of two values in array

### Language: C

Write a program which reads from the keyboard an integer value n followed by n double values as elements of an array with not more than 20 elements. Write also a function with the prototype:

double sum25(double v[], int n);

which computes and returns the sum of the elements in v at position 2 and position 5. Check that positions 2 and 5 are valid within v, if not then print a corresponding message on the screen. In this case the function should return the value −111. Print the sum on the screen.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 3.9: input6 Testcase 3.9: outputsum=0.000000

1.5

1.5

1.5

1.5

1.5

-1.5

## Problem 3.10 Return changes by reference

Write a program which reads two float values from the keyboard. Then write three functions. The first function should return the product of the two float values and should have the prototype:

float product(float a, float b);

The second function should return the product of the two float values by reference and should have the prototype:

void productbyref(float a, float b, float *p);

The third function should add 3 to the first float and 11 to the second float and return the change by reference. It should have the prototype:

void modifybyref(float *a, float *b);

Show that the calls of the first two functions have the same effect. Also show what is the effect of calling modifybyref.

You can safely assume that the input will be valid.

## Problem 3.11 Working with strings

Write a program where you can enter two strings called one and two from the keyboard. The string should be able to contain spaces. The program should do the following:

1. Print on the screen the lengths of both strings,
2. Print on the screen the concatenation of one with two,
3. Declare a third string, copy correctly two into it and print the third string to the screen,
4. Compare the two strings two and one and print a corresponding message to the screen,
5. Read a character c from the keyboard and search for c in two. The position of the first occurrence of c within two should be printed to the screen. If the character is not contained in the string then print a corresponding message on the screen.

For solving this problem use the string functions from string.h.

Learn how to use them with the help of the man pages.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 3.11: inputfirst string hello world l Testcase 3.11: outputlength1=12 length2=11 concatenation=first stringhello world

copy=hello world one is smaller than two position=2

## Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CH-230-A-Assignment 3 Loops, Arrays, Functions and Strings
30 \$