0

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?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
JackV
  • 218
  • 2
  • 12

2 Answers2

1

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.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Mozzis
  • 421
  • 4
  • 7
  • Could someone explain the int main(int argc, _TCHAR* argv[]) for me because i dont get whats in the brackets. also is there a reason all the other functions are above main now – JackV Feb 04 '15 at 01:37
  • 1
    @JackV: You can just ignore it and replace it with `int main()`. – Bill Lynch Feb 04 '15 at 01:42
  • 1
    @JackV: And for your second question: http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c – Bill Lynch Feb 04 '15 at 01:43
  • 1
    You need to put the function declarations before they are used. Some compilers will let you compile if you don't but then you won't be sure that the compilation is correct. As @BillLynch mentioned, the recursive call to main was bad. Don't ever do that. Each time it was called, a new array was created on the stack (as well as a new execution context.) – Mozzis Feb 04 '15 at 01:52
  • Thanks code worked and i understand it so thanks Mozzis and Bill Lynch – JackV Feb 04 '15 at 01:53
1

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;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173