不同数字数组 [英] Distinct numbers in array

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

问题描述

我不知道该怎么做。请帮我code或告诉我什么课本查找什么的;我需要code来完成这个程序,我会爱我在寻找什么解释。

I have no idea what to do. Please help me with code or tell me what textbook to look up or something; I need code to finish this program and I would love an explanation of what I'm looking at..

#include<iostream>
using namespace std;

int main()
{    
   short num[100], size, //declare an array of type short that has 100 elements
      unique[100], number,  // declare a second array to help solve the problem; number counts the number of unique values
      k;   // loop control variable; may need other variables
   cout<<"enter the number of values to store in the array\n";
   cin>>size;
   cout<<"enter the "<<size<<" values to be used as data for this program\n";
   for(k=0; k<size; k++)
      cin>>num[k];

   // print the contents of the array
   cout<<"\nthere are "<<size<<" values in the array\n";
   for(k=0; k<size; k++)
      cout<<num[k]<<’ ‘; // there is one space between each number in the display
   cout<<endl;  // cursor moved to next output line

   cout<<"the program will count the number of different (distinct) values found in the array\n";   

   //************************************************************
   //Put the code here that counts the number of unique values stored in the 
   //array num.  The variable number will contain the count.
   //************************************************************

   cout<<endl<<number<<" unique values were found in the "<<size<<" element array\n";
   // pause the program to see the results
   system("pause");
   //return 0;       
}

我必须做这两件事情之一,我不知道他们是什么意思?

I have to do one of these two things and I don't know what they mean?

算法 - 独特的阵列是用来帮助找到解决方案,用于避免计数的任何值超过一次
 设置数量为0 - 这再presents数据集中的不同值的数量;也用作独特阵列下标
 循环从0到大小由一个,则进行通过数据的连续元素(NUM)阵列
 &NBSP;&NBSP;当前数组元素的非数组变量(SV)存储值
 &NBSP;&NBSP;设置event_flag为0
 &NBSP;&NBSP;循环从0到数个,继续通过一系列独特
连续元素  &NBSP;&NBSP;&NBSP;&NBSP;如果SV等于独特阵列
当前元素  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;设置event_flag 1
 &NBSP;&NBSP;&NBSP;&NBSP;中断(停止)内环
 &NBSP;&NBSP;最终的内环
 &NBSP;&NBSP;如果event_flag等于0(值未发现独特的阵列,而不是previously计)
 &NBSP;&NBSP;&NBSP;&NBSP;存储SV在独特的数组元素
数量  &NBSP;&NBSP;增量数
价值  外循环
结束  解决方案 - 可变号码包含不同值的数据阵列中的计数

Algorithm – unique array is used to help find the solution, used to avoid counting any value more than one time
Set number to 0 - this represents the number of distinct values in the data set; also used as a subscript in the unique array
Loop from 0 to size by one, proceeding through successive elements of the data (num) array
  Store value of current array element in non-array variable (SV)
  Set event_flag to 0
  Loop from 0 to number by one, proceeding through successive elements of unique array
    If SV is equal to current element of unique array
      Set event_flag to 1
    Break (stop) inner loop
  End of inner loop
  If event_flag is equal to 0 (value not found in unique array and not previously counted)
    Store SV in element number of unique array
  Increment the value of number
End of outer loop
Solution – the variable number contains the count of distinct values in the data array

迭代算法
 算法,不使用event_flag(循环控制变量可以用来确定是否发生事件)

Alternate Algorithm
Algorithm that does not use the event_flag (loop control variable can be used to determine if event occurred)

算法 - 独特的阵列是用来帮助找到解决方案,用于避免计数的任何值超过一次
 设置数量为0 - 这再presents数据集中的不同值的数量;也用作独特阵列下标
 循环从0到大小由一个,则进行通过数据的连续元素(NUM)阵列
 &NBSP;&NBSP;当前数组元素的非数组变量(SV)存储值
 &NBSP;&NBSP;循环从0到数个,继续通过一系列独特
连续元素  &NBSP;&NBSP;&NBSP;&NBSP;如果SV等于独特阵列
当前元素  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;中断(停止)内环
 &NBSP;&NBSP;最终的内环
 &NBSP;&NBSP;如果内环的循环控制变量等于数的值(SV未发现独特阵列和不previously计)
 &NBSP;&NBSP;存储SV在独特的数组元素
数量  &NBSP;&NBSP;增量数
价值  外循环
结束  解决方案 - 可变号码包含不同值的数据阵列中的计数

Algorithm – unique array is used to help find the solution, used to avoid counting any value more than one time
Set number to 0 - this represents the number of distinct values in the data set; also used as a subscript in the unique array
Loop from 0 to size by one, proceeding through successive elements of the data (num) array
  Store value of current array element in non-array variable (SV)
  Loop from 0 to number by one, proceeding through successive elements of unique array
    If SV is equal to current element of unique array
      Break (stop) inner loop
  End of inner loop
  If loop control variable of inner loop is equal to value of number (SV not found in unique array and not previously counted)
  Store SV in element number of unique array
  Increment the value of number
End of outer loop
Solution – the variable number contains the count of distinct values in the data array

我把这个在我的:

//************************************************************
//Put the code here that counts the number of unique values stored in the array num. The variable number will contain the count.
for(k=0; k<size; k++)
num=SV;
event_flag=0;
for(k=1; k<number; k++)
if(SV=unique)
return true;
return false; 
//************************************************************

它不工作,效果显着。

It's not working, obviously.

推荐答案

这是我的code,它似乎工作

This is my code, it seems to work

//************************************************************
//Put the code here that counts the number of unique values 
//stored in the array num. The variable number will contain the count.

number = 0;
for (k = 0; k < size; ++k)
{
       short sv = num[k];
       short event_flag = 0;
       for (int i = 0; i < number; ++i)
       {
               if (sv == unique[i])
               {
                       event_flag = 1;
                       break;
               }
       }

       if (event_flag == 0)
       {
               unique[number] = sv;
               ++number;
       }
}

有关替代,

他是我的code,它似乎工作

his is my code, it seems to work

//************************************************************
//Put the code here that counts the number of unique values 
//stored in the array num. The variable number will contain the count.

number = 0;
for (k = 0; k < size; ++k)
{
       short sv = num[k];
       int i;
       for (i = 0; i < number; ++i)
               if (sv == unique[i])
                       break;

       if (number == i)
       {
               unique[number] = sv;
               ++number;
       }
}

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

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