我想要可以存储1000000整数的2D数组 [英] I want 2D array that can store 1000000 integers

查看:55
本文介绍了我想要可以存储1000000整数的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我声明为int a[1000000][1000000];,然后我得到的错误为array size is too large.
然后我遇到了同样的错误,却追上了long a[1000000][1000000].
我该如何解决这个问题?
我的程序是

I declared as int a[1000000][1000000]; Then I am getting error as array size is too large.
Then I chaged as long a[1000000][1000000], though getting same error.
How can I solve this problem?
my program is

#include<stdio.h>  
#define size 100000           
  int main()
  {
  
 //int* a;
//a=(int *)malloc(1000);
  
  long a[size][size],c,d,n,i,j,q1=0,q2=0,q3=0,q4=0,will=1,b[20][20],k=0;
  char p;
  //printf("enter the number of co ordinate");
  scanf("%d",&n);
  for(i=0;i<n;i++)>
  for(j=0;j<2;j++)
  scanf("%d",&a[i][j]);
  void quadrant(int temp1,int temp2)
  {
                   
         if(temp1>=0&&temp2>=0)
        q1++;
      else if(temp1>=0&&temp2<=0)
        q2++;
      else if(temp1<0&&temp2<0)
        q3++;
      else if(temp1<=0&&temp2>=0)
         q4++;
 
   }
//printf("enter no of operations");
scanf("%d",&will);
                
while(will>0)
{
p=getchar();
scanf("%c",&p);
switch(p)
{
case ''x'': 
                scanf("%d %d",&c,&d);
                  for(i=c-1;i<d;i++)>
                  a[i][1]=-(a[i][1]);break;
case ''y'': 
                scanf("%d %d",&c,&d);
                for(i=c-1;i<d;i++)>
                a[i][0]=-(a[i][0]);break;
case ''c'':       scanf("%d %d",&c,&d);
                for(i=0;i<=n-1;i++)
                {
                 int temp1,temp2;
                 temp1=a[i][0];
                 temp2=a[i][1];
              
                 quadrant(temp1,temp2);
                } 
               b[k][0]=q1;
               b[k][1]=q2 ;
               b[k][2]=q3;
               b[k][3]=q4; 
             
                  k=k+1; 
    q1=0;
    q2=0;
    q3=0;
    q4=0; 
                 
                  break;
case ''p'':   printf(" the co-ordinates are:\n");
               for(i=0;i<n;i++)>
               {
                 for(j=0;j<2;j++)
                {
              printf("%d   ",a[i][j]);
                 }
                printf("\n");
                 } 
                 break;                               
     
default: printf("enter the correct choice\n");break;
}
if(will==1)
for(i=0;i<k;i++)>
{
printf("%d %d %d %d\n",b[i][0],b[i][1],b[i][2],b[i][3]);
}
will--;
}
}
</stdio.h>

推荐答案

将数据存储在为随机访问而组织的文件中.保持文件打开,定义两个函数以按索引存储和检索整数.

—SA
Store data in the file organized for random access. Keep the file opened, define two function to store and retrieve integer by index.

—SA


1)请参阅我对您关于分段错误的其他问题的回答.
2)您非常需要回过头来了解有关二维数组的更多信息.

1,000,000 x 1,000,000是1,000,000,000,000(1万亿)个条目. "int"是每个4字节= 4 TB的内存(没有很多系统会让您拥有这么大的程序:).当您将其更改为"long"时,每个项目现在需要8个字节,从而使内存需求增加了一倍.

1)您没有在程序中使用[x,0]和[x,1]以外的第二维,所以您认为还需要其他999,998个条目的原因就是我.

2)尽管可以存储1,000,000个整数"(仅需要4MB的内存),但您可能需要考虑磁盘文件和其他方式来存储大量数据并在视图"中而不是在一个大内存块中对其进行处理的其他方式.

3)由于这显然是家庭作业,因此请询问您的讲师应提供多少实际数据,如果他/她建议输入100万个条目,请找出他们为什么认为合理,以及由谁来输入所有这些数据:)
1) See my answer to your other question about the segmentation fault.
2) you seriously need to go back and learn more about double dimensioned arrays.

1,000,000 x 1,000,000 is 1,000,000,000,000 (1 Trillion) entries. "int" is 4 bytes apiece = 4 TB of memory (not many systems will let you have a program that big :)). When you changed it to "long", you now need 8 bytes for each items, doubling your memory requirement.

1) you aren''t using the 2nd dimension in your program beyond [x,0] and [x,1] so why you think you need the other 999,998 entries is beyone me.

2) while it is possible to store 1,000,000 "ints" (only 4MB of memory needed), you might want to consider disk files and other ways of storing large amounts of data and processing it in "views" rather than in one big memory chunk.

3) Since this is obviously homework, ask your instructor what the realistic sizes of the data should be and, if he/she suggested 1 Million entries, find out why they think that''s reasonable and who''s going to type in all that data :)


这篇关于我想要可以存储1000000整数的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆