Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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: Friday

Explanation / 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; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote