Question4 Write code in C to do each of the following (a) Declare an integer var
ID: 3910176 • Letter: Q
Question
Question4 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]Explanation / Answer
(a) Syntax for declaring a variable is : datatype variable;
int max;
Initialize variable max to 0 : max = 0;
(b) Increment the variable max : max ++ (post- increment) or ++max (pre-increment)
(c) Declaring variable size array : int * vector;
Allocating dynamic space for 10 integers using malloc() : vector = (int *) malloc ( 10 * sizeof(int));
(d) char name[10], char contact[10]; // declare two strings name and contact
strcpy(contact, name); // this function copies name string to contact string, include header file <string.h>
(e) int x; //declare an integer x
if (( x % 2 == 0) && (x >0))
{
printf(" x is an even interger");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.