0

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;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Masih MK
  • 5
  • 5

1 Answers1

0

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

anotherOne
  • 1,513
  • 11
  • 20
  • Yh I tried That but then I get errors while using the functions in switch. It says i have to write int function and after writing int i get error on int. – Masih MK Dec 12 '20 at 19:38
  • I put the return type `void` in my example, but you should put whatever you need to put. – anotherOne Dec 12 '20 at 19:44
  • Thanks for ur time but I dnt get it cuz I am quite new to programming and its my first time working with functions. – Masih MK Dec 12 '20 at 19:53
  • Maybe this can help you understand more https://stackoverflow.com/questions/176118/when-is-it-ok-to-use-a-global-variable-in-c/176166#176166 and you seem to need clarification about passing and retuning arguments to function. Google those things – anotherOne Dec 12 '20 at 20:07
  • I tried to write my variables in to the function looks like this void talserie(int tal[99], int c); The in the main I have a switch where I used functions and by changing variables I get error on those functions. I tried to declare variables in main also but doesn't work. – Masih MK Dec 12 '20 at 20:41
  • Never mind got it fixed now. I forgot to write my variables into function parenthesis in main while using them for switch – Masih MK Dec 12 '20 at 20:55
  • @Masih MK I am glad you solved the problem. Anyway I wrote something more in the answer if you are interested – anotherOne Dec 12 '20 at 20:59
  • Thanks, It helped me figure out what I had to do. – Masih MK Dec 17 '20 at 18:27
  • Since I found your answer helpful, Would u mind taking a look into another question I asked? Its about the same assignment. – Masih MK Dec 17 '20 at 22:20
  • @MasihMK I did it :) – anotherOne Dec 17 '20 at 23:06