Search code examples
cglobal-variableslocal-variables

How to change the globale variables to local variables in C?


I am done with my assignment and everything works as I want but the thing is I am not allowed to have globale variables in this project therefor every thing should be in functions. Since I am new to C i don't really know how to get this work I mean How to change my global variables into local ones.

Appreciate Your help!

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

int tal[99] = { -1 };
int bubbles, byte, c, d, val;

/* Function for number gen*/
int talserie() {
    srand(time(NULL));
    for (c = 0; c < 100; c++) {

        tal[c] = rand() % 901;
        printf(" %d ", tal[c]);

        if ((c + 1) % 10 == 0)
            printf("\n");
    }
}
/* Funktion för bubble sort*/
int bubbel() {
    for (c = 0; c < (99); c++)
    {
        for (d = 0; d < 99 - c; d++)
        {
            if (tal[d] > tal[d + 1])
            {
                byte = tal[d];
                tal[d] = tal[d + 1];
                tal[d + 1] = byte;
            }
        }
    }

    for (c = 0; c < 100; c++) {
        printf(" %d ", tal[c]);
        if ((c + 1) % 10 == 0)
            printf("\n");
    }
}

/* Funktion för median-, max/min- och medelvärde*/
int varde() {
    printf("\nMaxvärdet är: %d", tal[99]);
    printf("\nMinvärdet är: %d", tal[0]);

    int total = 0;
    for (c = 0; c < 100; c++) {
        total = total + tal[c];
    }

    printf("\nMedelvärdet är: %d", total / 100);
    printf("\nMedianvärdet är: %d", ((tal[49] + tal[50]) / 2));
}

/*Funktion leta siffra*/
int siffra() {
    printf("\nSkriv in en siffra: ");
    scanf("%d", &val);
    d = 0;
    for (c = 0; c < 100; c++) {
        if (tal[c] == val) {
            d = 1;
            printf("\nFinns i talföljden på plats: ");
            if (c <= 9)
                printf(" Rad 1 och Kolumn %d\n", c + 1);
            else if (c > 9 && c <= 19)
                printf(" Rad 2 och Kolumn %d\n", (c + 1) - 10);
            else if (c > 19 && c <= 29)
                printf(" Rad 3 och Kolumn %d\n", (c + 1) - 20);
            else if (c > 29 && c <= 39)
                printf(" Rad 4 och Kolumn %d\n", (c + 1) - 30);
            else if (c > 39 && c <= 49)
                printf(" Rad 5 och Kolumn %d\n", (c + 1) - 40);
            else if (c > 49 && c <= 59)
                printf(" Rad 6 och Kolumn %d\n", (c + 1) - 50);
            else if (c > 59 && c <= 69)
                printf(" Rad 7 och Kolumn %d\n", (c + 1) - 60);
            else if (c > 69 && c <= 79)
                printf(" Rad 8 och Kolumn %d\n", (c + 1) - 70);
            else if (c > 79 && c <= 89)
                printf(" Rad 9 och Kolumn %d\n", (c + 1) - 80);
            else if (c > 89 && c <= 99)
                printf(" Rad 10 och Kolumn %d\n", (c + 1) - 90);
            break;
        }
    }
    if (d == 0);
    {
        printf("\n%d Finns inte i talföljden", val);
    }
}
/* Main funktion med switch meny*/
int main()
{
    while (val != 5) {
        printf("\n1. Generera en talföljd med 100 tal mellan 0-900.");
        printf("\n2. Sortera talföljden i storleksordning.");
        printf("\n3. Räkna ut medelvärde, median och maxvärde.");
        printf("\n4. Sök efter valfritt tal.");
        printf("\n5. För att avsluta\n");
        printf("Skriv in ett val (1-5): ");



        scanf("%d", &val);

        switch (val) {

        case 1:
            talserie();
            break;

        case 2:
            if (tal[0] == -1)
                printf("\nFel! Generera en talföljd först!\n");
            else
                bubbel();
            break;

        case 3:
            if (tal[0] == -1) /* Arrayen innehåller -1 i [0] innan talföljden genereras"*/
                printf("\nFel! Generera en talföljd först!\n");
            else if (tal[0] <= tal[1] && tal[1] <= tal[2] && tal[2] <= tal[3])

                varde();
            else
                printf("\nFel! Sortera talföljden i storleksordning först!\n");
            break;

        case 4:
            if (tal[0] == -1)
                printf("\nFel! Generera en talföljd först!\n");
            else if (tal[0] <= tal[1] && tal[1] <= tal[2] && tal[2] <= tal[3])
                siffra();
            else
                printf("\nFel! Sortera talföljden i storleksordning först!\n");
            break;

        }
    }
    return 0;
}

Solution

  • I speak in general, then you'll make the particular changes in your program.

    Let's say that you use one of your global variables in the function foo(). If you want your variable to not being global anymore, then you need to pass it to foo() as one of its arguments.

    So you would declare the variable as a local variable in the function that will call foo()

    void caller( void )
    {
        int local;
       
        foo( local );
    }
    
    void foo( int var )
    {
    ...Some code...
    }
    

    That was a pass by value, if you need foo() to modify the variable then you need to pass by reference

    void caller( void )
    {
        int local;
       
        foo( &local );
    }
    
    void foo( int *pointerToVar )
    {
    ...Some code...
    }
    

    EDIT: here an example to answer your comment. In main you will have

    int main()
    {
        int tal[99], c;
    
        talserie(tal, c);
        ...
    }
    

    And your function will be

    talserie(int tal[99], int c)
    {
        ...
    }
    

    But if you don't need to use c in main, you can declare it directly into talserie and you pass only tal