CPT109 C programming and SW engineering 1 Lab Practice 5 – Arrays
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CPT109 C programming and SW engineering 1
Lab Practice 5 - Arrays
Use your preferred compiler to investigate the programming exercises below. This laboratory concerns programs for the manipulation of arrays. You will also familiarise yourself with pointers.
Exercise 1 |
What is wrong with this declaration of a string? (You may try printing it to see what happens)
#include <stdio.h>
int main(){
char name[] = {‘F’,’a’,’n’,’t’,’a’,’s’,’t’,’i’,’c’};
printf("%s \n", name);
}
Exercise 2 |
What is returned by the following code:
#include <stdio.h>
int main(){
char *adr = “hello” ;
int i;
for(i=0; i<3; i++)
putchar(adr[i]);
printf("\n");
i=0;
while(adr[i])
putchar(adr[i++]);
}
Exercise 3 |
Write an appropriate declaration for each of the following variables:
1. digits - an array of 10 integers
2. rates – an array of 6 floats
Exercise 4 |
What will the following program print if you enter the number 9 from the keyboard when requested?
Compile and run the program to see if you were right.
Write the values of variables index and month after each of the last two statements is executed
#include <stdio.h>
main()
{
constint days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int index;
int month;
printf("Enter month\n");
scanf("%d",&month);
index = --month;
printf( "Month %2d has %d days\n", ++month, days[index]);
}
Exercise 5 |
Declare an array of six integers and initialise it to the values 1, 2, 4, 8, 16, and 32.
Exercise 6 |
Declare an array of 100 integers and initialise the last element with – 1. Don’t worry about the other elements.
Exercise 7 |
Write a program that declares and initialises an array of five floats and then copies the contents of the array into another array (Remember,the second array should also be declared before you can use it). Next compute the average of the set of five values. Use the array elements to compute the average not the numbers.
The program should print the five values stored in the second array on one line separated by commas and spaces, and the average value, as follows:
Example output:
1.34, 2.83, 17.82, 127.12, 0.12
The average is: 29.846
Exercise 8 |
Consider the following two-dimensional array declared as follows:
float t[3][4];
Write a program that initialises the array t using values of your choice and calculates the sum of the values of its elements.
Exercise 9 |
What is printed by the following program?
#include<stdio.h>
int main()
{
int ref[] = {9, 10, 20, 12, 101, 32};
int *ptr;
int index;
for(index=0, ptr=ref+5; index <6; index++, ptr--)
printf(“%d %d\n”, ref[index],*ptr);
}
Exercise 10 |
Write a function with return-type void, which determines the maximum and minimum values of an array of any size. The function should take four arguments as inputs: the array, its dimension, the maximum value, and the minimum value. The prototype of the function maybe:
void maxmin (intt[], intn, int *admax, int *admin)
Write a program that uses the function and test it for the input array
intt[8] = {2, 5, 7, 2, 9, 3, 9, 4};
2023-12-25