1)Use function GetUserInfo to get a user\'s information. If user enters 20 and H
ID: 639683 • Letter: 1
Question
1)Use function GetUserInfo to get a user's information. If user enters 20 and Holly, sample program output is:
#include <stdio.h>
#include <string.h>
void GetUserInfo(int* userAge, char userName[]) {
printf("Enter your age: ");
scanf("%d", userAge);
printf("Enter your name: ");
scanf("%s", userName);
return;
}
int main(void) {
int userAge = 0;
char userName[30] = "";
/* Your solution goes here */
printf("%s is %d years old. ", userName, userAge);
return 0;
}
2)Complete the function to replace any period by an exclamation point. Ex: "Hello. I'm Miley. Nice to meet you." becomes:
#include <stdio.h>
#include <string.h>
void MakeSentenceExcited(char* sentenceText) {
/* Your solution goes here */
}
int main(void) {
const int TEST_STR_SIZE = 50;
char testStr[TEST_STR_SIZE];
strcpy(testStr, "Hello. I'm Miley. Nice to meet you.");
MakeSentenceExcited(testStr);
printf("%s", testStr);
return 0;
}
3)
Write a function SwapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40}
becomes {40, 20, 30, 10}. The array's size may differ from 4
#include <stdio.h>
/* Your solution goes here */
int main(void) {
const int SORT_ARR_SIZE = 4;
int sortArray[SORT_ARR_SIZE];
int i = 0;
sortArray[0] = 10;
sortArray[1] = 20;
sortArray[2] = 30;
sortArray[3] = 40;
SwapArrayEnds(sortArray, SORT_ARR_SIZE);
for (i = 0; i < SORT_ARR_SIZE; ++i) {
printf("%d ", sortArray[i]);
}
printf(" ");
return 0;
}
Explanation / Answer
1.
#include <stdio.h>
#include <string.h>
void GetUserInfo(int* userAge, char userName[]) {
printf("Enter your age: ");
scanf("%d", userAge);
printf("Enter your name: ");
scanf("%s", userName);
return;
}
int main(void) {
int userAge = 0;
char userName[30] = "";
GetUserInfo(&userAge, userName);
printf("%s is %d years old. ", userName, userAge);
return 0;
}
2.
#include <stdio.h>
#include <string.h>
void MakeSentenceExcited(char* sentenceText) {
int i=0;
while(sentenceText[i] != '')
{
if(sentenceText[i] == '.')
sentenceText[i] = '!';
i++;
}
}
int main(void) {
const int TEST_STR_SIZE = 50;
char testStr[TEST_STR_SIZE];
strcpy(testStr, "Hello. I'm Miley. Nice to meet you.");
MakeSentenceExcited(testStr);
printf("%s", testStr);
return 0;
}
3.
#include <stdio.h>
void SwapArrayEnds(int *arr, int size)
{
int temp = arr[0];
arr[0] = arr[size-1];
arr[size-1] = temp;
}
int main(void) {
const int SORT_ARR_SIZE = 4;
int sortArray[SORT_ARR_SIZE];
int i = 0;
sortArray[0] = 10;
sortArray[1] = 20;
sortArray[2] = 30;
sortArray[3] = 40;
SwapArrayEnds(sortArray, SORT_ARR_SIZE);
for (i = 0; i < SORT_ARR_SIZE; ++i) {
printf("%d ", sortArray[i]);
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.