Starting from:

$25

CH-230-A-Assignment 3 Solved


Assignment 3 - Loops, Arrays, Functions and Strings

•    The problems of this assignment must be solved in C or C++ (instruction in each problem).

 Testcase 3.1: input

1.25

-8

0

4
Testcase 3.1: output

Input is invalid, reenter value

Input is invalid, reenter value

1.250000

1.250000

1.250000

1.250000
Problem 3.2 Writing characters

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.

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] != ’\0’; ++idx)

/* do nothing */ return idx;

}

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

}
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.

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

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: input

4

3

$
 Testcase 3.7: output

$$$

$$$$

$$$$$
$$$$$$

You can safely assume that the input will be valid.

Problem 3.8 Computing sum and average                                                             (1 point)

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.

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: input

6
Testcase 3.9: output

sum=0.000000
 1.5

1.5

1.5

1.5

1.5

-1.5

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.


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: input

first string hello world l
Testcase 3.11: output

length1=12 length2=11 concatenation=first stringhello world
copy=hello world one is smaller than two position=2

More products