-1

I am still a beginner to the world of programing in c lang and i would like if i get your help!

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main()
{
    int week;

    /* Input week number from user */
    printf("Enter week number(1-7): ");
    scanf("%d", &week);

    switch(week)
    {
        case 1: 
            printf("Monday");
            break;
        case 2: 
            printf("Tuesday");
            break;
        case 3: 
            printf("Wednesday");
            break;
        case 4: 
            printf("Thursday");
            break;
        case 5: 
            printf("Friday");
            break;
        case 6: 
            printf("Saturday");
            break;
        case 7: 
            printf("Sunday");
            break;
        default: 
            printf("Invalid input! Please enter week number between 1-7.");
    }

    return 0;
}

Thats the question that i am trying to do:

"Write a C program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday."

the only thing that is missing for me currently is that i want to make the usuer enters a random number then generate random number between 1 and 7

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

4 Answers4

5

Modulo operator % could be what you're looking for. On scanf() your user will enter a custom value that will be stored inside week. Then, you need to map this value to a number from 1 to 7.

Using week % 7, you will have a value in the range [0,6], then you add 1 to fit your [1,7] range.

Quentin
  • 724
  • 7
  • 16
3

Here is a demonstrative program that shows how random numbers from 1 to 7 inclusively can be generated.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) 
{
    srand( ( unsigned int )time( NULL ) );
    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        int value = rand() % 7 + 1;

        printf( "%d ", value );
    }

    putchar( '\n' );

    return 0;
}

The program output might look like

1 2 7 5 2 4 4 4 1 6 

Or another example

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) 
{
    enum DayOfWeek 
    { 
        Monday = 1, Tuesday, Wednesday,  Thursday, Friday, Saturday, Sunday
    };      
    srand( ( unsigned int )time( NULL ) );
    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        int day_of_week = rand() % 7 + 1;

        switch( day_of_week )
        {
        case Monday: 
            puts( "Monday" );
            break;
        case Tuesday: 
            puts( "Tuesday" );
            break;
        case Wednesday: 
            puts( "Wednesday" );
            break;
        case Thursday: 
            puts( "Thursday" );
            break;
        case Friday: 
            puts( "Friday" );
            break;
        case Saturday: 
            puts( "Saturday" );
            break;
        case Sunday: 
            puts( "Sunday" );
            break;
        }
    }

    putchar( '\n' );

    return 0;
}


    putchar( '\n' );

    return 0;
}

The program output might look like

Monday
Thursday
Saturday
Sunday
Friday
Monday
Friday
Wednesday
Thursday
Wednesday

The program can look simpler if to introduce an array of days' names.

For example

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) 
{
    const char * month_name[] =
    {
        "", "Monday", "Tuesday", "Wednesday",  "Thursday", "Friday", "Saturday", "Sunday"
    };

    srand( ( unsigned int )time( NULL ) );
    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        int day_of_week = rand() % 7 + 1;
        puts( month_name[day_of_week] );        
    }

    putchar( '\n' );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

Very basically starting from this I think you can make it work:

int main(void)
{
    int seed = time(0);
    srand(seed);
    int n = rand() % 7 + 1;
    printf("%d", n);
}

This will generate from 1 to 7, be careful if you plan to use it as index in some sort of array because C arrays start indexing from 0.

rand with seed does not return random if function looped explains why you should call srand only once in your application.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
1

This is merely a comment, intended to illuminate the idea brought out in naccyde's answer. You do not want a giant switch to decide what to print. Use an array. eg:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv) {
        const char * const days[] = {
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday"
        };
        int x = argc > 1 ? strtol(argv[1], NULL, 0) : 1;
        if( x < 1 || x > 7 ) {
                fprintf( stderr, "Invalid input: %d.  Must be Integer in [1,7]", x);
                exit(EXIT_FAILURE);
        }
        puts(days[x-1]);
        return EXIT_SUCCESS;
}
William Pursell
  • 204,365
  • 48
  • 270
  • 300