0% found this document useful (0 votes)
44 views

Pe 10-12

The document provides instructions for three programming exercises to be completed in groups for an AMAT150 laboratory meeting. Exercise 10 involves dynamically allocating memory to an array, accepting user input to populate the array, computing the sum, and printing the results. Exercise 11 defines and populates a structure with different data types. Exercise 12 implements a nested structure to represent a rectangle defined by two points, computes the diagonal length given the points, and prints the results. Useful math functions and a sample code are also included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Pe 10-12

The document provides instructions for three programming exercises to be completed in groups for an AMAT150 laboratory meeting. Exercise 10 involves dynamically allocating memory to an array, accepting user input to populate the array, computing the sum, and printing the results. Exercise 11 defines and populates a structure with different data types. Exercise 12 implements a nested structure to represent a rectangle defined by two points, computes the diagonal length given the points, and prints the results. Useful math functions and a sample code are also included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Exercises 10, 11, and 12

For AMAT150 Laboratory Meeting on 18 November 2014


General Instructions:

The activities are by group to be determined during lecture.

You are allowed to open your notes (electronic or not).

Work with your own group. Having identical programs is considered cheating.

Each program should be saved in a file with .c or .cpp extension and with name,
Lastname1_Lastname2_Lastname3_PE#
replace:

Lastname1, Lastname2, and Lastname3 with the surnames (in


alphabetical order) of the group members

# in PE# with the programming exercise #

Email your files to


[email protected]

Email before the laboratory period ends (before 4pm). Late submissions will not
be accepted.

Global declarations are not allowed except for constants, prototypes, and structure
definitions.

Implement structured programming.

#10: Dynamic Allocation. Write a program that dynamically allocates memory to an array
of n numbers.
o

The value of n must be asked from the user.

The program should ask the user for n numbers and store these numbers to the
array.

The program should compute for the sum of the n numbers.

The program should print the array and the sum of the numbers.

#11: Simple Structure. Write a program that defines a structure with four members (one
string, one integer, one double, one character) and has one instance. The members of
the structure must be given values by asking these from the user. The program must
print the values.

#12: Diagonal of a Rectangle. Write a program that implements a structure within a


structure and computes the length of the diagonal of a rectangle. A rectangle can be
defined using two points (each is a coordinate composed of the values x and y) that
connect to form the diagonal of the rectangle.
o

The program must implement a structure that contains structures (represents the
points) as members. This structure will represent the rectangle.

The program must ask the user for the coordinates of the two points.

Given the two points to form the diagonal, the program must compute for the
length of the diagonal.

The program must print the two points and the computed length of the diagonal.

Useful built-in functions under the math.h library file:


abs(n) a function that accepts an int value n and returns the absolute value (int type)
of n.
labs(n) a function that accepts an long int value n and returns the absolute value
(long int type) of n.
fabs(n) a function that accepts an double value n and returns the absolute value
(double type) of n.
sqrt(n) a function that accepts an double value n and returns the square root value
(double type) of n. This function only works with positive values of n.

Sample code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main () {
int a = -16;
int b;
double root;
b = abs(a);
printf (The absolute value of %d is %d, a, b);
root = sqrt(b);
printf (\nThe square root of %d is %.2lf, b, root);
getch();
return 0;
}
Note: For double values, you should use %lf as conversion specifier for printing. But, if the value
is still covered by the range for float, you can use %f.

You might also like