## Basic C Programming

Introduction

In this assignment you will write small C programs. Each C program should be submitted as a

standard C source code file. You can submit your files separately or zipped together into one file.

Please note that the computer program should comply with the commenting and formatting rules

as has been done in class. For example, there should be a header for the whole program that gives

the author’s name, class name, date, and description. End braces should be commented, and there

are alignment and indenting requirements as discussed. Please ask if you have any questions.

Program #1 (15 points)

Write a program for each case (a to c). Each program will ask the user for an integer number n and

a character ch. The output generated depends on the value of n and the character ch.

For example, say that n = 5 and ch = ‘*’:

(5 points) Program 1.a) will display

* *** ***** ******* ********* (10 points) Program 1.b) will display:

* * * * * * * * * * * * * * * Notice: the number of spaces between each ‘*’ is increasing at each row.

(20 points) Program 1.c) will display

1* 2* 3* 4* 5*

2* 3* 4* 5* 1*

3* 4* 5* 1* 2*

4* 5* 1* 2* 3*

5* 1* 2* 3* 4*

CS 1325 Introduction to Programming

HW1: Basic C

Page 2

Program #2

Write a program to evaluate any function of order 5 or less at a given interval [xmin, xmax], and

display a table with the values of x, and f(x).

INPUT:

The program will ask the user to enter

a) (5 points) The function’s order f_order.

If the order is greater than 5 or smaller than 1, an error message should be displayed and the program ends,

Otherwise, the program will ask for the respective coefficients.

b) (5 points) The values for xmin and xmax

If the value for xmax is smaller or equal to xmin, an error messages should be displayed and the program will end.

Assume that f(x) is defined as f(x) = ax5 + bx4 + cx3 + dx2 + ex + f.

(20 points) If the f_order is:

5, then the program will ask for coefficients a, b, c, d, e and f;

4, then the program will ask for coefficients b, c, d, e and f;

3, then the program will ask for coefficients c, d, e and f;

2, then the program will ask for coefficients d, e and f;

1, then the program will ask for coefficients e and f;

OUTPUT:

The output is formatted as a table. The first column will display the values of x, and the second

column the values of f(x). The first row should always have the value xmin for x, and f(xmin) for

f(x). Last row should always have the value xmax for x, and f(xmax) for f(x).

The table will look like:

x f(x) ———–|——————- xmin | f(xmin) xmin +dx | f(xmin+dx) xmin +2dx | f(xmin+2dx) xmin +3dx | f(xmin+3dx) xmin +4dx | f(xmin+4dx) . | . | xmax | f(xmax) ——————————

CS 1325 Introduction to Programming

HW1: Basic C

Page 3

Where

xmin is the value entered by the user, and

f(xmin) is the result of evaluating the function when x=xmin

dx is the increment size, and is defined by 𝑑𝑥 = (𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛)/10 Use 2 decimals for f(x) values.

Table formatting: 10 points

Function evaluation: 20 points

Include a report in a Word file that shows

a) List all the variables, constants(literals, symbolic and named) in your program 2). (5 points)

*Optional [Extra Points]:

Include in the word file, plots of f(x) in the range [-13,13] for each valid value of f_order (10 points). You pick the coefficients for each case.

If you do not have access to MATLAB you can also use OCTAVE or SciLab.

You are allowed to use the function float pow( float, int), found in <math.h>, to calculate the powers of x.

Topics covered: Basic C, Expression evaluation, operators, flow control statements (loops,

switch-case, if-else, etc) and MATLAB as optionals