Basic C programming structures Question 4 Write code in C to do each of the foll
ID: 3908719 • Letter: B
Question
Basic C programming structures Question 4 Write code in C to do each of the following (a) Declare an integer variable called max with initial value 0 (b) Increment the variable max (c) Declare a variable size array vector and assign a dynamically allocated [1 mark] [1 mark] space for 10 integers to it [2 marks] (d) Copy the values of a C string, name to a second C string contact. You can assume there is sufficient space in contact to hold name [2 marks] (e) Write a single if condition that tests whether an integer x is even and greater than zero. 4 marks] [Total for Question 4: 10 marks] C Programming Question 5 (a) Write a segment of C code to prompt a user for a number and then print all of the odd numbers between 1 and that number. [4 marks] (b) Write a function that calculates and returns the surface area of a right cir cular cone. The equation is Area :: ?r(r + v1? + r*) where h is the height of the cone and r is the radius [10 mark] (c) Write a program that prompts the user to enter up to 10 numbers and stores these numbers in an array. The program should stop accepting input when the user types -1 and then print out the numbers the user has entered [10 marks] [Total for Question 5: 24 marks] Question 6 (a) Write a function, columnSwitch, that takes a 2D array of any size, the num ber of rows in the 2D array, and two column numbers (c1 and c2) and swaps the elements in column c1 with the elements in c2. For example given the array shown below, the number of rows as 3 ,and the columns 0 and 2. 12 3 4 5 6 7 8 9 the resulting array should be 3 2 1 6 5 4 9 8 7 [10 marks] (b) Write a program that asks the user for the number of rows and columns and then randomly generates the values for a 2D array of the specified size The program should then call the function columnSwitch to switch the first and last column of the 2D array [10 marks] Total for Question 6: 20 marks]Explanation / Answer
Question 4 with all subparts
---------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main()
{
//declare vaiable max and initilaized to 0
int max=0;
//increment variable max
max=max+1;
//variable size array vector
int *vector;
//dynmaic allocation of 10 integer
vector=(int *)malloc(10*sizeof(int));
//copy string to another
char name[]="chegg";
char contact[50];
int i;
for(i=0;name[i]!='';i++)
contact[i]=name[i];
contact[i]='';
int x;
//single if condtion to check x is even and greater than zero
if(x%2==0 && x>0)
printf("x is even and greater than zero");
}
----------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.