2013-10-27 60 views
1

我在生成一個整數數組時遇到了問題。程序使用-32429173等數字填充數組的數百個單元,而不是數字從0到3的3個單元(例如)。也許問題出在分配內存的錯誤方式上?這是代碼的錯誤部分。 Thx提前幫忙。生成一個整數數組

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

使用隨機()代替蘭特()。 「男人3隨機」。 –

+0

如果打印'數組[我]'裏面你'產生()'函數,怎麼樣了沒有結果的樣子? – PhillipD

+0

方式太多的代碼,爲什麼你不隔離你有問題的部分? –

回答

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

還有一些事情你可以做到這一點不重複,即所以你不會得到兩個黑桃王牌。但現在,你有更大的問題...

你的代碼,因爲有太多的問題要構建。如果您發佈了一個小的,可構建的部分,並解決了具體問題,那麼可以更好地解決。

+0

陣列現在完全充滿,但我得到了錯誤「訪問衝突讀取位置0x0031A000」 – D3migod

2

我想你應該初始化maxnumb

+0

你的回答很符合評論。這不是問題的答案。 – ryyker

0

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