阵列和指数 [英] arrays and index

查看:101
本文介绍了阵列和指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能输入数字到重复的条目这样一个数组不会被插入?例如,如果我试图把 6 入阵,那么 3 进入第二的第一个条目,然后, 6 进入第三个, 6 应该被拒绝(因为它会在数组中已存在)。请张贴在C ++中完成code。我的继承人code你能告诉我什么是失踪?

 的#include<&iostream的GT;使用命名空间std;
诠释的main()
{
  INT X,Y;
  INT编号;
  INT ARR [5];  为(X = 0; X小于5)
  {
    COUT<<进入nmber<< ENDL;
    CIN>>数;
    布尔取代= TRUE;
    对于(Y = 0; Y< X; Y ++)
    {
       如果(号码!= ARR [Y])
       {
         COUT<< 尝试下一次<< ENDL;
         取代=假;
         打破;
       }
    }    如果(替换)
    {
      ARR [X] =号;
      X ++;
    }
}
返回0;}


解决方案

你有太多的 X ++'和你没有preSET 改编(比错误也许更多的风格)

你怎么知道它不工作?
 (把一些调试code 内,如果(数字==常用3 [Y])如果(更换

How can I enter numbers into an array in such a way that duplicate entries will not be inserted? For example, if I try to put 6 into the first entry of the array, then 3 into the second, then 6 into the third, 6 should be rejected (since it will already exist in the array). Please post the complete code in C++. heres my code can you tell me whats missing?

#include <iostream>

using namespace std;
int main()
{
  int x,y;
  int number;
  int arr[5];

  for (x=0;x<5;)
  {
    cout<<"enter nmber"<<endl;
    cin>>number;
    bool replace=True;
    for (y=0;y<x;y++)
    {
       if (number != arr[y])
       {
         cout << "try next time"<<endl;
         replace=False;
         break;
       }
    }

    if (replace)
    {
      arr[x]=number;
      x++;
    }
}
return 0;

}

解决方案

you have too many x++'s and you don't preset arr (maybe more style than error)

how do you know it's not working? (put some debug code inside of if (number == arr[y]) and if (replace)

这篇关于阵列和指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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