JackV
JackV

Reputation: 218

Edit arrays within function and return array to main in C

Im writing a program to give the user options whether they want to:

  1. Add random numbers to an array
  2. Print array
  3. Search for an element in an array
  4. Left shift array

These have to be in separate functions and it needs to b recursive and until the user wants to finish it keeps running my code is:

int main()
{
    int array[M][N];
    int ans;

    puts("Please enter what you would  like to do:\n
            1: Create an array with random values\n
            2: Print Array\n
            3: Search for a number in an array\n
            4: Shift each value to the left");
    scanf("%d",&ans);
    switch(ans) {
        case 1:
            PopulateRandom2D(array);
            break;
        case 2:
            PrintArray(array);
            break;
        case 3:
            LinearSearch2D(array);
            break;
        case 4:
            LeftShift(array);
            break;
        default:
            puts("Goodybye");
            return 0;
    }
    main();
    return 0;
}

void PopulateRandom2D(int array[][N])
{
    int r,c;
    srand(time(NULL));

    for(r = 0; r < M; r++) {
        for(c = 0; c < N; c++) {
            array[r][c] = 1 + (rand() % (M * N));
        }
    }
}

After i call the function i need the user to enter another command and call a different function from the user input. Ive been experimenting by first hitting 1 so it fills the array with numbers and then hitting 2 so it will hopefully print out that array but all i get are huge numbers in the array. I don't think the function is editing the array in main correctly so main doesn't get the array with random values but how do i fix this?

Upvotes: 0

Views: 107

Answers (2)

Bill Lynch
Bill Lynch

Reputation: 81936

You're currently recursively calling main(). In each iteration of calling main(), you'll create a new array on the stack.

This isn't what you want.

Instead, wrap your code in a while(true) { ... } loop. It would look something like this:

#include <stdbool.h>

int main()
{
    int array[M][N];
    int ans;

    while (true) {
        puts("Please enter what you would  like to do:\n
                1: Create an array with random values\n
                2: Print Array\n
                3: Search for a number in an array\n
                4: Shift each value to the left");
        scanf("%d",&ans);
        switch(ans) {
            case 1:
                PopulateRandom2D(array);
                break;
            case 2:
                PrintArray(array);
                break;
            case 3:
                LinearSearch2D(array);
                break;
            case 4:
                LeftShift(array);
                break;
            default:
                puts("Goodybye");
                return 0;
        }
    }
    return 0;
}

Upvotes: 1

Mozzis
Mozzis

Reputation: 419

The code below works:

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

const int M = 10;
const int N = 20;

void PrintArray(int array[][N]) {}
void LinearSearch2D(int array[][N]) {}
void LeftShift(int array[][N]) {}
void PopulateRandom2D(int array[][N])
{
    int r, c;
    srand(time(NULL));

    for (r = 0; r < M; r++) {
        for (c = 0; c < N; c++) {
            array[r][c] = 1 + (rand() % (M * N));
        }
    }
}


int main()
{
    int array[M][N];
    int ans;

    while (true)
    {
        puts("Please enter what you would  like to do:"
            "\n1: Create an array with random values"
            "\n2: Print Array"
            "\n3: Search for a number in an array"
            "\n4: Shift each value to the left"
            "\n5: Quit");
        scanf("%d", &ans);
        switch (ans) {
        case 1:
            PopulateRandom2D(array);
            break;
        case 2:
            PrintArray(array);
            break;
        case 3:
            LinearSearch2D(array);
            break;
        case 4:
            LeftShift(array);
            break;
        default:
            puts("Goodybye");
            return 0;
        }
    }
    return 0;
}

I leave it to you to fill in the other functions.

Upvotes: 1

Related Questions