简体   繁体   English

使用 Array 生成随机数

[英]Generate random numbers with Array

I am learning C and I keep mixing up array and matrix.我正在学习 C 并且我一直在混合数组和矩阵。 and I can't seem to understand where I am doing wrong with my code.而且我似乎无法理解我的代码在哪里做错了。 I have made 2 different version of it and the only feedback I am getting is, I have mixed array with matrix.我已经制作了 2 个不同的版本,我得到的唯一反馈是,我将数组与矩阵混合。

I was wondering if anyone could help me understand exactly where I went wrong, since I am not getting anywhere with my prof and his explanation about it.我想知道是否有人可以帮助我准确了解我哪里出错了,因为我的教授和他对此的解释没有得到任何帮助。 (Sorry in advanced) (对不起,提前)

This is the first code, But both of them are really similar.这是第一个代码,但它们都非常相似。

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

int main()
{
    int array[100];
    int max = 0, min = 1;
    int fro;

    fro = (int)time(NULL);
    srand(fro);

    for (int i = 0; i < 100; i++) {
        array[i] = rand() % (400 + 1 - 100) + 100;
        if (array[i] <= min) {
            min = array[i];
        }
        if (array[i] >= max)
            max = array[i];
    }
     
    printf("Array\n");
    for (int row = 0; row < 10; row++) {
        for (int col = 0; col < 10; col++) {
            printf(" %i ", array[row * 10 + col]);
        }
        printf("\n");
    }
    return 0; 
}

This is another version, but I got similar feedback as the first one.这是另一个版本,但我得到了与第一个版本相似的反馈。 I am mixing up array and matrix..我正在混合数组和矩阵..

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

int main()
{
    int tal[10][10];
    int array[100];
    int max = 0, min = 1;
    int fro;

    fro = (int)time(NULL);
    srand(fro);

    // Version 3 of the array slump generation

    for (int row = 0; row < 10; row++) {
        //printf("Yttre loop row %i\n", row);
        
        for (int col = 0; col < 10; col++) { // row = 1, col = 0
            tal[row][col] = rand() % (400 + 1 - 100) + 100;
            // printf("tal[%i][%i] = %i\n", row, col, tal[row][col]);

            // Get max
            if (tal[row][col] >= max) {
                max = tal[row][col];
            }
            // Get min
            if (tal[row][col] <= min) {
                min = tal[row][col];
            }
            // printf("tal[%i][%i] = %i\n", row, col, tal[row][col]);
        }
    }
    
    // Printar ut hela matrisen i row-major order
    for (int row = 0; row < 10; row++) {
        for (int col = 0; col < 10; col++) {
            printf(" %i ", tal[row][col]);
        }
        printf("\n");
    }
    
    return 0;
}

I mean both of them works and I think I am using array:/ We haven't even gone trough matrix yet...我的意思是它们都有效,我想我正在使用数组:/我们甚至还没有通过矩阵...

The notion of a matrix is not an ISO C concept.矩阵的概念不是ISO C概念。 However, you can use an array to denote a matrix by imposing imposing additional linear algebra constraints.但是,您可以通过施加额外的线性代数约束来使用数组来表示矩阵。 There are several linear algebra libraries for C. C 有几个线性代数库 However, be prepared for some people use the term matrix loosely to mean any two-dimensional array, because they look similar in most presentations.但是,请准备好一些人松散地使用术语矩阵来表示任何二维数组,因为它们在大多数演示文稿中看起来相似。

Your first example is an int array[100] which stores array 100 of int .您的第一个示例是一个int array[100] ,它存储数组 100 的 int You then access it by 10 row and 10 col ;然后通过 10 row和 10 col访问它; since 10 * 10 = 100 <= 100 that you've reserved for it, this is entirely valid.因为 10 * 10 = 100 <= 100 你已经为它保留了,所以这是完全有效的。 One might prefer this representation because it is explicitly contiguous , but has no way for the compiler to bounds-check.人们可能更喜欢这种表示,因为它是显式连续的,但编译器无法进行边界检查。

Your second example is int tal[10][10] , which is array 10 of array 10 of int , all in the same block of memory .您的第二个示例是int tal[10][10] ,它是int 的数组 10 的数组 10都在 memory 的同一块中 It's the same 100-element array, but accessed differently, as you've done.它是相同的 100 元素数组,但访问方式不同,就像您所做的那样。 This is also valid, and I think more what your teacher was asking.这也是有效的,我想你老师问的更多。

The one you treat more like a matrix will be more like a matrix.您将其视为矩阵的将更像矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM