生成整数数组

时间:2013-10-27 16:05:35

标签: c arrays visual-studio-2010 random

我在生成整数数组时遇到了问题。该程序用数字(如-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;
}

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

首先 ,这是填充数组的更简单方法。

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;
}  

你也可以采取一些措施使这种不重复,即你不会得到两个黑桃王牌。但就目前而言,你有更大的问题......

您的代码,因为有太多问题需要构建。如果您发布一个具有特定问题的小型可构建部分,则可以更好地解决该问题。

答案 2 :(得分:0)

printArray()putsFile()可能会在数组大小之外打印。

在确定文件中int* enterFile()的数量之前,

size未将int设置为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");
  }
}