1. Read in a length in inches, convert to centimetres and write out the result. (1 inch = 2.54 cm).
2. Read in four numbers and write out the average.
3. Travel expenses are calcualted as 12p per mile. Read in the start and finish mileometer readings and write out the miles travelled and expenses payable.
4. A farmer has a field B metres wide, L metres long. The field yields C cubic metres of grain per hectare (1000 square metres). The farmer stores his grain in a number of cylindrical silos, H in height and R in radius. Read in the parameters B, L, C, H and R; write out:
(a) the number of completely full silos.
(b) the height of grain in any unfilled silo.
The volume of each silo can be calculated as 3.142 x radius squared x height
1. Write a program to read the number of electricity units used by a consumer and write out the total cost. These are the rates: 5.2p per unit for the first 100 units; 3.0p per unit for the next 300 units; and 1.4 per unit for the remaining units.
Solution
2. Write a program to read out three numbers and write the smallest.
3. Travel expenses are paid as follows: 15p per mile for cars up to and including 1.5 litre engines; 20p per mile for cars above that spec.. Write a program to calculate travel expenses which takes as input car engine size and distance travelled.
4. Write a program to read a 24-hour clock time and write out the time in a 12 -hour format, with 'am' and 'pm' as necessary. The input should be validated (i.e. the program should check whether it is valid, with hours between 0 - 23 and minutes between 0 - 59).
1.Add N input numbers and write out the result. Read the number of inputs
first. Solution
2. Write the Fibonnacci sequence (0, 1, 1, 2, 3, 5, 8, 13 .) starting from 0 up to a user-defined number.
3. Write a conversion table for metres, inches and feet (1m = 39 inches, 1 foot = 12 inches), for 0- 5 metres in steps of 0.2 metres.
4.Write on the screen a large cross composed of asterisks. Each arm of the cross should
be 5 asterisks wide and 10 asterisks long. Do not use printf(.) statements that write more than one character at a time!
1. Write a function that will convert between the Fahrenheit and Celsius scales (celsius = (fahr-32)*(5/9) ). Write a program using that function to print a conversion table using the function.
2. Write a version of the Fibonacci program above using a function that takes a starting point and the last point and prints out the sequence.
3. Write a version of the electricity bill program above with a function that takes the prices for the three bands (0-100,101-400,401-) and calculates the total charge. It should take user input values for the band prices and total units.
4. Write a function that will take an input pressure amplitude in N/m2 and convert it into SPL(dB) values. Write a program that will printout amplitude values for every 6dB increment from 0 to 120dB.
(a) SPL = 20 log10 (amp/[20 x 10-6])
(b) use the maths library function log10( ) [header file: math.h]
1. Write a program to read in 8 integers and write them in reverse order. Use loops to read and write the numbers.
Solution
2. Read in six digits, check it is a valid date and write the date with the month in full.
3. Sort a sequence of 10 integers into asceding order. Here's a simple sorting algorithm for a sequence of N elements:
(a) Find the location I of the largest element from A[0] to A[N].
(b) Interchange A[I] with A[N]
(c) Decrease N by 1
(d) If N = = 0 finish else repeat from (a)
1. Write a program to generate a score file for Csound. Play the C major scale from middle-C upwards for two octaves, speeding up (starting with 2-second notes) and crescendo from p to ff (max amplitude). We will assume p4 for amplitude and p5 for frequency. The first i-statement line would read (as an example):
i1 0 2 1000 8.00
Provide a simple csound instrument for it and run csound from the c-program (using the stdlib.h function system())
2. Write a program that scrambles a text file according to a certain encoding by exchanging each alphabet letter for a different one. Use an external text file to provide the key (code) and try the program with different text files, encoding and decoding them.
1. Write a program that a takes an input raw soundfile and attenuates its amplitude
by -6dB.
2. Create a program that ring-modulates an input soundfile with a sinewave at a given fixed rate.
The program should take raw-format files, which it will assume to be mono, 16-bit at 441000 Hz.
The program will take four arguments, mod freq (Hz), mod index (0-1), input and output filenames.
A sinewave at a certain frequency is described by the following expression
sine(n) = sin(2*PI*fr*n/SR)
where n is the sample number (0,1,..., N-1)[N is the total number of samples]
PI is 3.1415....
fr is frequency
SR is the sampling rate
Ring-modulation can be achieved by multiplying two signals together.
You can use this raw soundfile (44.1kHz, mono, pcm,signed, 16-bit): sf1
1. Write a program that opens a file and checks if it is a RIFF-Wave
or an AIFF file. If so, it should print the following information,
taken from the soundfile header:
Number of channels
Sampling rate
Precision (8-,16-,24-bit etc.)
Length in bytes of audio data
Duration in seconds.
This program should try to take the format information from the file
extensions: if ".wav" it will assume (and check) it is a RIFF-Wave file; if
".aif", it will take it to be in the AIFF format. An error message should be
printed if the file is not in any of these two formats. Use libsndfile as a means to open soundfiles.
2. Using libsndfile write a version of problem 2 from problems VII (the AM one),
which can write to any of the supported PCM soundfile formats (depending on
the soundfile extension).
Libsndfile header file for Intel Macs: sndfile.h
The libsndfile header file (sndfile.h), binary and sources can be downloaded from here. For MS Visual C++ users, this package includes the pre-compiled DLL, link-library (LIB) and header file.
This archive contains the header file, a DLL and a link library. All of these should be copied to your Visual C++ project file. The project settings should include libsndfile.lib as one of the library modules.
GCC
On Mac OSX and Windows with MINGwin, the compiler is gcc and the library binary can be built from sources. This is done by using the commands ./configure and make (in that order) on the top-level sources directory.
Here you can find a simple MIDI playback library for Windows, with four functions, which you can use with your exercises:
int midinit(int dev) : opens midi device number dev
int listdevices() : prints a midi device list to the console
int noteplay(int note, int vel, int channel, int length) : plays a midi note for length milliseconds
init midiclose() : closes the device.
Here's the header and the source file. To use these functions you will need to add the source file to your command-line, give the compiler the path of the header file and add the win multimedia (winmm) library to the build:
gcc -o ... midiplay.c -I. -lwinmm
the -I flag adds a directory to the include search path, in this case '.' is added (the current directory), where midiplay.h should be. The other flag, -lwinmm, passes the winmm library to the linker.
And here is the mini-sequencer I showed you in class: play.c
A MSVC project file package containing all 2SEM examples and files is also available here.
You can download the VST2 SDK and examples (with MSVC project files) here: vst_sdk2_4_rev2