【问题标题】:Generating an array of integers生成整数数组
【发布时间】:2013-10-27 16:05:35
【问题描述】:

我在生成整数数组时遇到了问题。该程序用 -32429173 之类的数字填充数组的数百个单元格,而不是数字从 0 到 3 的 3 个单元格(例如)。也许问题出在分配内存的错误方式上?这是代码的错误部分。感谢您提前提供帮助。

int* generate()
    {

        int maxnumb;
        int i;
        scanf_s("%d",&size);    //size of an array
        scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
        int* array=(int*)calloc(size,sizeof(int));
        for (i=0;i<size;i++)
          array[i] = rand() % maxnumb + 1;
        return array;
    }

这是完整的代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
int size;

void swap(int* elem1,int* elem2) //swap elements
{
    int temp;
    temp=*elem1;
    *elem1=*elem2;
    *elem2=temp;
}
void bublesort(int* array,int size) //bublesort
{
    for (int j=1;j<size-1;++j)
    {
    for (int i=0;i<size-j;++i)
    {
        if ((array[i])>(array[i+1]))
        swap(&array[i],&array[i+1]);
    }

    }
}
int* enterHand() //handle entering
{

    int i;
    scanf_s("%d",&size);
    int* array=(int*)calloc(size,sizeof(int));
    for (i=0;i<size;i++)
    {
        scanf_s("%d",&array[i]);
    }
    return array;
}
int* enterFile() //entering from the file
{
    int i;
    int singlenumb;
    FILE* foo;
errno_t err;
    err=fopen_s(&foo,"input.txt","r");
    if( err == 0 )
  {
      printf( "The file 'input.txt' was opened\n" );
  }
  else
  {
      printf( "The file 'input.txt' was not opened\n" );
  }
    while (!feof(foo))  
    {
        fscanf_s(foo, "%d", &singlenumb);
        size++;
    }

    size-=1; 
    int* array=(int*)calloc(size,sizeof(int));
    rewind(foo);
    i=0;
    while (i!=size)  
    {

        fscanf_s(foo, "%d", &array[i]);
        i++;
    }
    fclose(foo);
    return array;
}
int* generate()
{

    int maxnumb;
    int i;
    scanf_s("%d",&size);    //size of an array
    scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
    int* array=(int*)calloc(size,sizeof(int));
    for (i=0;i<size;i++)
      array[i] = rand() % maxnumb + 1;
    return array;
}
void putsFile(int* array, int size)
{
    int i=0;
    int k;
    FILE* fooo;
    fopen_s(&fooo,"output.txt","w");

    while (i!=size)
    {
        for (k=0; k<10; k++)
        {
            fprintf(fooo,"%d ", array[i]);
            i++;
        }
        fprintf(fooo,"\n");
    }
    fclose(fooo);
}
void printArray(int* array, int size)
{
    int i=0;
    int k;
    while (i!=size)
    {
        for (k=0; k<10; k++)
        {
            printf("%d ", array[i]);
            i++;
        }
        printf("\n");
    }
}
int main()
{
    int choice;
    int* pntr;
printf("choose a type of filling an array\n1 = handle filling\n2 = filling from the file\n3 = generating\nenter the number...\n");
scanf("%d", &choice);
switch (choice)
{
case 1: {pntr=enterHand();} break;
case 2: {pntr=enterFile();} break;
case 3: {pntr=generate();} break;
default: {pntr=NULL;} 
}
bublesort(pntr,size);
printf("choose a type of typing an array\n1 = console\n2 = file\nenter the number...\n");
scanf("%d", choice);
switch (choice)
{
case 1: {printArray(pntr, size);} break;
case 2: {putsFile(pntr, size);} break;
default: {printf("you entered the wrong number");} 
}
return 0;
}

【问题讨论】:

  • 使用 random() 代替 rand()。 “男人 3 随机”。
  • 如果你在你的generate()函数中打印array[i],结果是什么样子的?
  • 代码太多了,你为什么不隔离你有问题的部分?
  • 你还没有为你的 int 分配内存 *
  • @ryyker 我只想将数组的指针复制到“pntr”。但它不起作用......我刚刚得到另一个数组......我该怎么办?

标签: c arrays visual-studio-2010 random


【解决方案1】:

我认为你应该初始化你的 maxnumb

【讨论】:

  • 您的回答非常适合评论。这不是问题的答案。
【解决方案2】:

首先,这里有一个更简单的填充数组的方法。

int randomGenerator(int min, int max);
int main(void)
{
    int array[100]//or any size
    for(i=0;i<sizeof(array)/sizeof(array[0]);i++)
    {
        array[i]=randomGenerator(1,3);//will issue only 1, 2 or three (as you requested)
    }                                 //change min and max for wider range
    return 0;
}

int randomGenerator(int min, int max)  //set the range of desired numbers in array
{
    int random=0, trying=0;

    trying = 1;         
    while(trying)
    {
        srand(clock());
        random = (rand()/32767.0)*(max+1);
        (random >= min) ? (trying = 0) : (trying = 1);
    }
    return random;
}  

您还可以采取一些措施来使这种情况不再重复,即您不会得到两张黑桃 A。但是现在,你有更大的问题......

您的代码本来就有太多问题需要构建。如果您发布一个小的、可构建的部分,并带有特定的问题,它可以得到更好的解决。

【讨论】:

  • 数组现在已完美填充,但我收到错误“访问冲突读取位置 0x0031A000”
【解决方案3】:

printArray()putsFile() 可能打印超出数组大小。

int* enterFile() 在确定文件中int 的数量之前未将size 设置为0,因此使用了可能意外的非零大小。

[编辑] size 在全局未初始化空间中被隐式设置为 0。不过,建议在 enterFile() 中明确设置为 0。

#if 0
  // The value of size is dependent on program history or uninitialized.
  while (!feof(foo)) {
    fscanf_s(foo, "%d", &singlenumb);
    size++;
  }
  size-=1; 
#else
  size = 0;  // Reset size to 0
  while (1 == fscanf_s(foo, "%d", &singlenumb)) {
    size++;
  }
#endif

建议:

void printArray(int* array, int size) {
// Also in void putsFile(int* array, int size)
  int i=0;
  while (i < size) {
    int k;
    for (k=0; k<10; k++) {
      printf("%d ", array[i]);
      i++;
      // Add
      if (i >= size) break;
    }
    printf("\n");
  }
}

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 2017-04-14
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多