如何从数组中选择一个数组进行操作? [英] How to choose an array from an array for manipulation?

查看:83
本文介绍了如何从数组中选择一个数组进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想编写一个程序来选择一个数组(V [S])中具有最多元素数(1至10)的数组(v1至v8),并将其放在另一个数组(V_prime)中.
我将一直这样做,直到数组V_prime达到最大值或从1到10的元素数被选择为止,但我不确定如何编写它.任何建议将不胜感激.
我试图输出例如[v7 [S],然后依次选择v6 [S],v4 [S],v5 [S],依此类推,直到选择了所有顶点或选择了所有元素(1到10)

Hi,
I want to write a program to choose the array(v1 to v8) with the most number of elements(1to 10) within an array(V[S]) and put it in another array(V_prime).
I will do so until the array V_prime is at maximum or the number of elements from 1 to 10 are chosen but I not sure how to write it. Any advice will be appreciated.
I am trying to output for eg [v7[S] is chosen first follow by v6[S],v4[S],v5[S]and so on until all vertex are chosen or all elements are chosen(1 to 10)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define S 20
int main()
{
    int V_prime[S]={0},V1[S];
    int array[S],M1[S];
    int largest,i,j,k;
    int points=10,sensors=8;
    int v1[S] = {1};
    int v2[S] = {5};
    int v3[S] = {2,4};
    int v4[S] = {3,4,7};
    int v5[S] = {6,9};
    int v6[S] = {5,8,9};
    int v7[S] = {6,7,10};
    int v8[S] = {10};
    int M[S] = {v1[S],v2[S],v3[S],v4[S],v5[S],v6[S],v7[S],v8[S]};
    int h1[S] = {v1[S],v2[S],v3[S]};
    int h2[S] = {v7[S],v8[S]};
    int h3[S] = {v3[S],v5[S],v6[S],v7[S]};
    for(i=0;i<15;i++)
    V1[i] = M[i];           //copy arrayM[S] to V1[S]
    for(i=0;i<points;i++)   //set array[S] to have elements from 1 to points
    {   array[i] = i;
    }
/* Select the largest element and remove it and add it to V_prime*/
        do
    {
        largest = sizeof(V1[0]);        //set V1[0] as the vertex with the largest elements
        for(i=1;i<sensors;i++)          //run loop depending on number of vertices
        {
            if(sizeof(V1[i]) >= largest)//if i vertex is larger than the first one set it as largest
                largest = sizeof(V1[i]);
            else
                largest = sizeof(V1[0]);    //otherwise V1[0] still largest
        }
        printf("%d has the most elements of %d\n",i,largest);//printout
        V_prime[i] = V1[i];             //largest element is setas 1st element of V_prime
/*----------------Remove the largest element-----------------*/
        for(j=0;j<i;j++)
        {
            M1[j] = V1[i];          //V1[0] to V1[i-1] copy to M1[0] to M1[j-1]
        }
        for(j=i;j<sensors;j++)
        {
            M1[j] = V1[i+1];    //V1[i] is ignored and continue with the next elements for appending
        }
        sensors = sensors-1;    //one vertex chosen thus decrement by 1
        for(k=0;k<sensors;k++)      //new M[S] after removing 1 element
        {
            M[k] = M1[j];
            printf("abc%d\n",M[k]);
        }
    }while (sensors !=0);

}

推荐答案

[edit]抱歉,我错过了太多数组[/edit]

但是,您仍在使用无效的方法来查找数组中的元素数量或最大数量. sizeof()运算符返回实体的字节大小,因此sizeof(V1[0])将始终返回4,因为这是32位程序中int的大小.您的所有数组也都定义为包含20个元素,因此我不确定这与您要尝试执行的操作如何匹配.您需要初始化每个数组以指示最后一个有效条目的位置,然后使用该值对代码中所需的条目进行计数.您还需要比较存储在每个元素中的实际值,以查看哪个值最大.我仍然不确定您要在这里实现什么,因此很难提供任何进一步的建议,但是我想使用C ++ vector会更有用.
[edit]sorry I missed that, too many arrays[/edit]

However, you are still using an invalid method to find the number of elements in an array, or the largest number. The sizeof() operator returns the size in bytes of an entity, so sizeof(V1[0]) will always return 4 as that is the size of an int in 32-bit programs. All of your arrays are also defined as having 20 elements so I am not sure how that fits with what you are trying to do. You need to initialise each array to indicate where the last valid entry is and then use that to count the entries you need within your code. You also need to compare the actual values stored in each element to see which is the largest. I am still not sure what you are trying to achieve here so it is a bit difficult to offer any further advice, but I would have though that using C++ vectors would be much more use.


这篇关于如何从数组中选择一个数组进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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