In: Computer Science
Part 1.1
Write a function whose prototype is
void exchange ( /*inout*/ int *p, /*inout*/ int *q )
that takes two pointers to integer variables and exchanges the values in these variables.
Part 1.2
Write a function whose prototype is
char lastChar ( /*in*/ const char *str )
that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c.
Part 1.3
Define an array of 35 of the Car structure variables.
Initialize the first three elements with the following data:
Make | Model | Year | Cost |
Ford | Taurus | 1997 | $ 21,000 |
Honda | Accord | 1992 | $ 11,000 |
Lamborghini | Countach | 1997 | $ 200,000 |
Part 1.1
//function to swap two integer variable using pointere
void exchange(int *p, int *q)
{
//temp variable declaration
int temp;
//swap the values
temp = *p;
*p = *q;
*q = temp;
}
The screenshot of the above source code is given below:
Part 1.2
//function to returns the last character in the string
char lastChar(const char *str)
{
int size=0;
while(str[i] != '\0')
size++;
size--;
return str[size];
}
The screenshot of the above source code is given below:
Part 1.3
//car structure
struct Car
{
//variable
char Make[20];
char Model[20];
int Year;
float Cost;
};
struct Car c[35];
//initialize the first car structure variable
strcpy(c[0].Make, "Ford");
strcpy(c[0].Model, "Taurus");
c[0].Year = 1997;
c[0].Cost = 21000;
//initialize the second car structure variable
strcpy(c[1].Make, "Honda");
strcpy(c[1].Model, "Accord");
c[1].Year = 1992;
c[1].Cost = 11000;
//initialize the third car structure variable
strcpy(c[2].Make, "Lamborghini");
strcpy(c[2].Model, "Countach");
c[2].Year = 1997;
c[2].Cost = 200000;