注册 登录
编程论坛 C++教室

C++ 数组问题

ming530108 发布于 2008-06-25 08:52, 1547 次点击
如何输入一个全为1的6行6列数组,要求,当输入数组的数不是1时,要重新输入
14 回复
#2
zjl1382008-06-25 11:56
for(i=0;i<6;i++)
   for(j=0;j<6;j++)
      a[i][j]=1;
#3
hp521_kylin2008-06-25 22:47
2楼的好像忘记了判断~不过这个过程简单!!嘿嘿!!
int temp;
cin>>temp
if(temp)
{
 for(i=0;i<6;i++)
   for(j=0;j<6;j++)
      a[i][j]=temp;
}
#4
zjl1382008-06-25 22:53
不得不说楼上错了.
#5
ming5301082008-06-25 23:21
如果输入的是0或一呢??。。这种只是单单输入一个数时候有效的的吧??
要是要求输入0或一呢??那该如何判断。。
#6
冰镇柠檬汁儿2008-06-26 09:30
都错了
#7
安静的小羊2008-06-26 09:38
int temp;
int a[6][6];
for(int i=0;i<6;i++)
   for(int j=0;j<6;){
      cin>>temp;
      if(temp == 1){
      a[i][j] = temp;
      j++;
   }
}

这样行吧。
#8
sunlong2008-06-26 09:52
#include <iostream>
using namespace std;
int main()
{
    int temp=0;
    int count=0;
    int m[6][6];
    do
    {
        if(count==0)
        cout<<"input an integer number:";
        else cout<<"incorrect,input a new number:";
        cin>>temp;
        for (int i=0; i<6; ++i)
            for(int j=0; j<6; ++j)
        {
                m[i][j]=temp;

        }
        ++count;
    } while (temp!=1);
    for (int i=0; i<6; ++i)
        for(int j=0; j<6; ++j)
    {
            cout<<m[i][j]<<endl;
    }
    return 0;
}
看看这个行否
#9
zjl1382008-06-26 10:24
[bo][un]冰镇柠檬汁儿[/un] 在 2008-6-26 09:30 的发言:[/bo]

都错了


我的哪里错了?

你觉得我的还会跑出除 1 之外的值吗?

[[it] 本帖最后由 zjl138 于 2008-6-26 10:25 编辑 [/it]]
#10
ming5301082008-06-26 19:50
,都不行丫。。。。
#11
中学者2008-06-26 19:59
#include<iostream>
using namespace std;
int main()
{
    int col_,row_;
    cout<<" 输入二维数组的行列"<<endl;
    cin>>row_>>col_;
    int size_,n;
    int *array= new int[n=size_=row_*col_];
    int value;
    row_=col_=0;
    while(n>0)
    {
       cin>>value;
       if(value==1)
      {
         array[size_*row_+col_]=value;
         if(++col_==size_)
            ++row_,col_=0;
          --n;
       }
      }
    delete [] array;
    return 0;
}
#12
kongwei2542008-06-27 22:00
一群强人
#13
byd9132008-07-04 14:22
没什么意思!
#14
guoqingchun2008-07-08 15:26
我的好用,调试过了。
#include<iostream.h>
void main(){
    int i,j;
    int a[6][6];
    for(i=0;i<6;i++){
        for(j=0;j<6;){
            cin>>a[i][j];
            if(a[i][j]==1)
                j++;
            else
                continue;
        }
        cout<<endl;
    }
    
}
1