简体   繁体   English

(c++) 将数组索引和它包含的元素从 function 移动到 function

[英](c++) moving array index and element it holds from function to function

I have to create multiple functions (no limit), main should create 75 random numbers.我必须创建多个函数(没有限制),main 应该创建 75 个随机数。 range{0 to 100}.范围{0 到 100}。 and in another function i need to add odd index elements(call it s2) and in another function add the even(s1).在另一个 function 中,我需要添加奇数索引元素(称之为 s2),在另一个 function 中添加偶数(s1)。 and another function that will print the answers to s1 and s2.另一个 function 将打印 s1 和 s2 的答案。 the actual question i was given:我得到的实际问题是:

Using C++ code, write a program to compute the product of the sums S1 and S2 of the even (indexes 0, 2, 4, 6, 8, ...) and odd (indexes 1, 3, 5, 7, 9, ...) elements, respectively, of a vector (or array) A of n = 75 random integers in the range [0,100].使用 C++ 代码,编写一个程序来计算偶数(索引 0、2、4、6、8、...)和奇数(索引 1、3、5、7、9、 ...) 元素,分别是向量(或数组)A 的 n = 75 个范围 [0,100] 中的随机整数。 First, A is created and initialized using 75 random integers in the function main.首先,在 function main 中使用 75 个随机整数创建并初始化 A。 Secondly, a function even_sum receives, as parameters, A and n, and computes and returns S1.其次,function even_sum 接收 A 和 n 作为参数,计算并返回 S1。 Thirdly, a function odd_sum receives, as parameters, A and n, and computes and returns S2.第三,function odd_sum 接收 A 和 n 作为参数,计算并返回 S2。 Lastly, a function finalize receives, as parameters, S1 and S2 and computes and prints the product of S1 and S2.最后,function finalize 接收 S1 和 S2 作为参数,计算并打印 S1 和 S2 的乘积。

so far, i finished main, inside it creates completly random numbers that are unique.到目前为止,我已经完成了 main,在它里面创建了完全随机的唯一数字。 i am having trouble moving the vales from main to another function. hope this make sense.我无法将 vales 从 main 移动到另一个 function。希望这是有道理的。 I am very new to coding, and would love inputs or critics to learn from.我对编码很陌生,很乐意从中学习输入或评论。

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
using namespace std;


void odd()

{
    
}

void fill(int array[], int length, int min, int max);//inital
int main()
{
    srand (unsigned(time(NULL)));
    cout<<"random 75 numbers are following: \n";
    int a[75];
    //void fill(int array[], int length, int min, int max);
    fill(a, 75, 1, 100);
    
    for (int i=0;i<75;i++)
    {
        printf("a[%d] = %d\n", i, a[i]);
            }
    
  
}

    void fill(int array[], int length, int min, int max)
{
        int new_num;
        bool unique;//verify if the new number is unique compared to array
        
        for(int i=0; i<length; i++)//generate number, make sure its unique
        {
            do
            {
                new_num=(rand()%(max-min+1))+ min;
                unique= true;
                
                for(int j=0; j<i;j++)
                    if (array[j]==new_num)

                        unique= false;
            }
            while(!unique);
            
            array[i]= new_num;
            void odd();
    }
   
  // game plan is to go from main to odd fuction to even to finialize and in finalize, cout/print the sums
}

The problem asks you to compute two sums that depends on A and then multiply those results, so in this situation, you may want to modularize your tasks by thinking those sums as "functions".该问题要求您计算取决于 A 的两个总和,然后将这些结果相乘,因此在这种情况下,您可能希望通过将这些总和视为“函数”来模块化您的任务。 Let me explain让我解释

  1. You want to compute the sum of odd-indexed numbers of A, so you take into account 2 main parameters: A and its length, right?你想计算 A 的奇数索引数之和,所以你考虑了 2 个主要参数:A 和它的长度,对吗? So let's do that:所以让我们这样做:
// Here, you will define the odd_sum function
int odd_sum(int array[], int length)
{
    // Here, you compute the odd-indexed values and accumulate them,
    // For example, you may declare a
    // int sum = 0;
    // and start adding the values within a for loop
    // Finally you return sum
    return sum;
}
  1. You want to compute the sum of even-indexed numbers of A, so you do the same as the previous one but with slight variations:您想要计算 A 的偶数索引数之和,因此您执行与前一个相同的操作,但略有不同:
// Here, you will define the even_sum function
int even_sum(int array[], int length)
{
    // Here, you compute the even-indexed values and accumulate them,
    // For example, you may declare a
    // int sum = 0;
    // and start adding the values within a for loop
    // Finally you return sum
    return sum;
}
  1. Finally, you want to get those values and multiply them, so it may not be difficult to do it inside your main function:最后,您想要获取这些值并将它们相乘,因此在您的主 function 中执行此操作可能并不困难:
int odd_sum(int array[], int length);
int even_sum(int array[], int length);

int main()
{
    int a[75];
    // Do your generating stuff
    int oddSum = odd_sum(a, 75);
    int evenSum = even_sum(a, 75);
    int product = oddSum * evenSum;
    printf("Product is %d", product);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM