C编程,从一个数组中的一个数组中选择元素并将其移动到另一个数组 [英] C Programming on selecting and moving elements from an array within an array to another array
本文介绍了C编程,从一个数组中的一个数组中选择元素并将其移动到另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我想编写一个程序来选择一个数组(V [S])中具有最多元素数(1至10)的数组(v1至v8),并将其放在另一个数组(V_prime)中.我将一直这样做,直到数组V_prime达到最大值或从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.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define S 15
int main()
{
int V_prime[100]={0},V1[S];
int array[S];
int i,j;
int points=10; //no of elements
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 V[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<S;i++)
V1[i] = V[i]; //copy arrayV[S] to V1[S]
for(i=0;i<points;i++) //set array[] to have elements from 1 to points
{ array[i] = i;
}
推荐答案
我强烈建议您不要使用#defineS.每当使用#define时,您都需要添加括号,并且需要避免#defines很短,因为它们肯定会引起问题.
另外,为什么要为所有数组指定大小为15?也许您应该使用双端队列或媒介来解决问题?或者,您不能最初调整数组的大小,而不能使用_countof()获得大小.
例如:int v1 [] = {1};
...
对于(long j = 0; j< _countof(v1); j ++)
...
I would strongly suggest against using #define S. Whenever you use #define you need to add paranthesis and you need to avoid #defines that are very short as they certainly will cause problems.
Also, why are you specifying a size of 15 for all of the arrays? Maybe you should use a deque or vector for your problem? Alternatively you can not size the arrays initially and use _countof() to get the size.
example: int v1[] = {1};
...
for (long j=0; j<_countof(v1); j++)
...
这篇关于C编程,从一个数组中的一个数组中选择元素并将其移动到另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文