-2

total C newbie here. Thanks for any help beforehand.

I am required to write a code which has the following properties:

  1. Asking user to input the number of rows and columns they would like in their 2-D array
  2. creates a 2-D array with that many rows and columns for storing integers
  3. fills the array with random numbers between 1 and 1000
  4. outputs the largest number in the array

This is where I could go for now:

#include <stdio.h>

int main(){

 int rows;
 int columns;

 scanf("%d", &rows);
 scanf("%d", &columns);
 }

What should I do? Sorry for the very open question, I am stuck and don't know what to do. A guideline will be perfect. Thanks :)

EDIT:

Thanks for the guideline, here is how I solved it: SOLUTION:

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

int main () {
    //Declare variables
    int a,b,k,l,big,z[100][100];
    //Ask user input
    printf("ENTER ROWS & COLUMNS\n");
    scanf("%d\n%d", &a, &b);
    //Randomize array values
    srand(time(NULL));
    big = 1;
    for (k=0;k<a;k++){
        for(l=0;l<b;l++){
            z[k][l]=rand()%1000 + 1;
            if(z[k][l]>big) big=z[k][l];
            printf("%d\t", z[k][l]);
        }
        printf("\n");
    }
    //Print biggest number
    printf("\nBIGGEST NUMBER IN THE ARRAY: %d", big);
    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Xavier99
  • 15
  • 5
  • Sorry but SO doesn't work that way. We help with problems in **your** code but we don't write code for you. – Support Ukraine Mar 30 '20 at 12:25
  • So you need to be more specific... where are you stuck? Do you know that program starts with: `int main(void) { ....};` So what is it that you can't solve? Getting user input? Creating a 2D array? Fill the array? .... – Support Ukraine Mar 30 '20 at 12:26
  • First try to implement #1 and print it back to standard output using `printf` to see whether the input was read properly. Then try to implement #2 and #3 and print the contents of the 2D array to see whether it worked. Then try to implement #4. If you have a **specific** problem with any of these steps, then you are welcome to ask that **specific** question. – Andreas Wenzel Mar 30 '20 at 12:31
  • It is unsafe to use use `scanf` in this manner. You may want to read this for further information: [A beginner's guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Mar 30 '20 at 12:34
  • `scanf("%d", &rows);` ==> `if (scanf("%d", &rows) != 1) exit(1);` – Support Ukraine Mar 30 '20 at 12:34
  • You may want to read this: [How to I ask a good question?](https://stackoverflow.com/help/how-to-ask). That page also contains a link about asking homework questions. – Andreas Wenzel Mar 30 '20 at 12:40
  • If you answer your own question, then please write your answer as an "official" answer to the question, instead of editing your question. See this page for more information: [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – Andreas Wenzel Mar 30 '20 at 14:32
  • I have reverted your edit to your question because you overwrote important details to your question. You can't expect people who find your question to have to look into the edit history to find the actual question. – Andreas Wenzel Mar 30 '20 at 15:19

2 Answers2

2

A guideline will be perfect.

okay... let's go...

  1. Asking user to input the number of rows and columns they would like in their 2-D array

Use scanf or better fgets followed by sscanf

  1. creates a 2-D array with that many rows and columns for storing integers

Use malloc (search the net for "how to correctly malloc a 2D array" or simply see Correctly allocating multi-dimensional arrays)

  1. fills the array with random numbers between 1 and 1000

Use srand, rand and the % operator

  1. outputs the largest number in the array

Iterate all elements of the array and compare against a running max.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Thank you for the guideline. Could solve it now but didn't use malloc. – Xavier99 Mar 30 '20 at 14:23
  • 1
    @Xavier99: Generally, if you believe an answer solved your problem, then please accept it. You can only accept one answer though. But if you have 15 reputation points, then you can upvote all answers that you find useful. If you believe no answer solved your problem, you can also write your own answer and accept it. – Andreas Wenzel Mar 30 '20 at 14:45
1

C does not support natively 2D arrays, you need to allocate an array of arrays. You don't know the size in advance, so you need to allocate the arrays memory dynamically using malloc.

For generating random numbers, you could use rand, but you need to set a seed first for the sequence of the pseudo-random integers using srand to get (possibly) a different number on every execution, or you can use rand_r to do the both operation with one function. Note that rand generate a random number between 0 and RAND_MAX, you need to use a trick with the modulus operator % to generate a random number in a specific range [min, max].

min + (rand() % (max - min + 1))

You can iterate over the arrays with two loops, to get the maximum number.

To know how to use these function, you can read the man pages: malloc rand

As a side note, you don't really need the 2D array to get the maximum number, you can calculate it directly with allocating any additional memory.


Your solution will work, but it's limited to 100 columns and 100 rows. If for instance the user enters higher numbers, your code will mostly crash. One solution is to validate the input and refuse numbers above 100 if you don't want to handle dynamic memory allocation, but in real world that is very much rare and dynamic memory allocation is a necessary.

You shouldn't name your variables with one-character names, especially if they live long. A good descriptive name like columnsNumber is preferred.

ahmad
  • 56
  • 1
  • 4