C++ Write a program that asks for a number from the user and prints which day of
ID: 3915101 • Letter: C
Question
C++
Write a program that asks for a number from the user and prints which day of the week that number corresponds to. The days are indexed from 0 (Sunday) to 6 (Saturday). Store the names of the days in an array and print the name of the day in two ways:
with pointer arithmetic;
with array indexing.
Before the program gets a value from the array, it must first check if the given day is greater than or equal to zero and less than 7. If not, it should print the message: "Error, no such day." Your version of the program must print the same result as the expected output.
#include <stdio.h>
int main()
{
/* your code */
return 0;
}
Example input 5 Example output Pointer version: Friday Array index version: FridayExplanation / Answer
#include int main() { char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int d; scanf("%d", &d); if(d < 0 || d > 6) { printf("Error, no such day. "); } else { printf("Pointer version: %s ", *(days+d)); printf("Array index version: %s ", days[d]); } return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.