Question

In: Computer Science

PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called...

PRACTICAL 10 C PROGRAMMING.

Question 1 - Reading into a dynamic array.

Write a program called temperatures01 that reads a (non-empty) sequence maximum daily temperatures. Your program should first ask for the number of temperatures to read and dynamically allocate an array just big enough to hold the number of temperatures you read. You should then read in the elements of the array using a loop. You then should print out the elements of the array in reverse order (from most recent to oldest).

Test Cases

count     temperatures                          output
3         15.0 17.0 19.5                        19.5 17.0 15.0
5         16.5 18.8 20.5 21.3 16.2              16.2 21.3 20.5 18.8 16.5

Question 2- Dynamic odds-and-evens

Problem Definition

Write a program called odds_evens01 that reads a (non-empty) sequence of integers. As for question 1 above, your program should first ask for the number of integers to read and dynamically allocate an array just big enough to hold the number of values you read.

You should then read in the elements of the array using a loop. Then your program must dynamically allocate two arrays one to hold all the even numbers from the array you have just read and one to hold the odd numbers from the array. Your code must allocate just enough space to each of the arrays to hold the odd and the even numbers. For example, given the input:

3 4 5 6 9 1 2 8 2

ou must allocate an array of 4 values for the odd numbers and an array of 5 values for the even numbers. Your program must then print the values in the odd array and the even array.

Test Cases

Two test cases for the program are:

input              output
3 4 5 6 9 1 2 9    3 5 9 1 9    4 6 2
1 1 1 2 2 3 8      1 1 1 3      2 2 8

Write two more test cases in a text file called: odds_evens01.txt and save it in the directory for this practical.

Coding

Implement the problem definition above in a file called odds_evens01.c. Compile your program with -Wall to help improve the quality of your code. Note, you will need to write a dedicated loop just to count the number of odd and even elements there are so you can allocate the arrays for these. Also note, you may find some code from your previous sessions helpful in writing this solution. Make sure you save your code into your week10practice folder.

Question 3 - A dynamically growing array

Problem Definition

This is based on your answer to question 1.

One problem with the solution to questions 1 and 2 of of this practical is that the user has to know, in advance, how many values they will enter so we can allocate an array of the right size. In some cases it is better for a program just to keep reading values until the user types in a finishing value such as: -100.0.

In this problem you are to save your code from question 1 to write a new program called temperatures02. Then modify the code to:

  • initally allocate an array to hold up to 5 temperatures.
  • prompt the user to enter temperatures and type the value -100.0 when they are finished.
  • if the user fills up the array your program should
    • dynamically allocate a new array which is double the size.
    • copy the old values across to the new array.
    • deallocate the old array.
    • continue reading into the new array.

After you finish reading your array your program should, as before, output the temperatures read in reverse (most recent to oldest).

Test Cases

As before two test cases for your program would be:

count     temperatures                          output
3         15.0 17.0 19.5                        19.5 17.0 15.0
5         16.5 18.8 20.5 21.3 16.2              16.2 21.3 20.5 18.8 16.5

Coding

Implement the problem definition above in a file called temperatures02.c. Compile your program with -Wall to help improve the quality of your code. Make sure you save your code into your week10practice folder. Note: your loop to read values should be a while loop that terminates when you read the stopping value of -100.0.  Inside your loop you will need to have a check to see if you have reached the end of the currently allocated array and have code to perform the new allocation, copying and deallocation. You might want to write a small program to implement and test the logic for allocation, copying and deallocation so that you are confident that you have the process working before integrating it into your solution.

Solutions

Expert Solution

Question 1:

#include <stdio.h>

int main()
{
int number;
printf("Enter size of the array : ");
  
scanf("%d", &number); //accept an integer from user
float arr[number];
for(int i=0;i<number;i++){
float temp;
printf("Enter number : ");
scanf("%f", &temp); //accept array elements integer from user
arr[i]=temp;
}

for (int i=number-1;i>=0;i--){
printf("%.1f ",arr[i]);
}
return 0;
}

------------------------------------------------------------------------------

Question 2

#include <stdio.h>

int main()
{
int number;
printf("Enter size of the array : ");
  
scanf("%d", &number); //accept an integer from user
int arr[number];
int odd_cnt= 0 ; // to keep track of how many odd integers are there
int even_cnt = 0; //to keep track of how many even integers are there
for(int i=0;i<number;i++){
int temp;
printf("Enter number : ");
scanf("%d", &temp); //accept array elements integer from user
if(temp %2 ==0){
even_cnt++;
}else{
odd_cnt++;
}
arr[i]=temp;
}
  
int odd[odd_cnt];
int even[even_cnt];
int i_odd = 0;
int i_even = 0;
for(int i=0;i<number;i++){
if(arr[i]%2 == 0){
even[i_even]=arr[i];
i_even++;
}else{
odd[i_odd]=arr[i];
i_odd++;
}
}
  
for(int i=0;i<odd_cnt;i++){
printf("%d ",odd[i]);
}
printf("\t");
for(int i=0;i<even_cnt;i++){
printf("%d ",even[i]);
}
return 0;
}
-------------------------------------------------------------------------

Question 3:

#include <stdio.h>

void printArray(double *array, int size){ //printing array in reverse order
for(int i=size-1; i>=0; i--){
printf("%.1lf ", array[i]);
}
putchar('\n');
}

int main(void){
int size = 5; //initializing size to 5
double *array = malloc(size * sizeof(double));
double temperature;
int i = 0;

while(1){
if(temperature == -100.0) //if input is -100 then get out of the loop
break;
if(i == size){
size *= 2;
array = realloc(array, size * sizeof(double)); //if size is greater than capacity then increase the size to double
}
printf("Enter number : ");
scanf("%lf", &temperature);
array[i] = temperature;
i++;
}
printArray(array, i);
free(array);
return 0;
}


Related Solutions

Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
In C programming, Thanks Write a program to determine which numbers in an array are inside...
In C programming, Thanks Write a program to determine which numbers in an array are inside a particular range. The limits of the range are inclusive. You have to write a complete "C" Program that compiles and runs in Codeblocks. (main.c) 1. Declare an array that can contain 5 integer numbers. Use as the name of the array your LastName. 2. Use a for loop to ask the user for numbers and fill up the array using those numbers. 3....
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT