开发者

C++ Greatest Number Verification

开发者 https://www.devze.com 2022-12-27 07:19 出处:网络
Hey guys, I was assigned to create a program that creates n arrays composed by 10 random integers. The the program should sum all the integers and display the result. After, it has to verify which of

Hey guys, I was assigned to create a program that creates n arrays composed by 10 random integers. The the program should sum all the integers and display the result. After, it has to verify which of the sums is the greatest and it has to display that array and the result. Im having troubles getting it don开发者_开发问答e and would like to get some help! Thanks once again. Here is my code so far:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double random(unsigned int &seed);
unsigned int seed = 5;
void generateData(int set[10]);
int sumData(int set[10]);
void checkData(int sumResult, int arrayNumber);
int main (int argc, char * const argv[]) {
    int arrayNumber, sumResult; 
    int set[10];
    do {
        cout << "Number of Arrays to Compare: " << endl;
        cin >> arrayNumber;
    } while (arrayNumber < 0);
    for (int i = 0; i < arrayNumber; ++i) {
        generateData(set);
        sumResult = sumData(set);
        cout << "Sum --> " << sumResult << endl;
        checkData(sumResult, arrayNumber);
    }
    return 0;
}

double random(unsigned int &seed) {
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER * seed) + INCREMENT) % MODULUS;
    return double(seed) / double(MODULUS);
}

void generateData(int set[10]) {
    for (int i = 0; i < 10; ++i) {
        set[i] = int (5 + 6 * random(seed));
        cout << set[i] << " || ";
    }
}

int sumData(int set[10]) {
    int sumTotal = 0;
    for (int i = 0; i < 10; ++i)
        sumTotal = sumTotal + set[i];
    return sumTotal;
}

void checkData(int sumResult, int arrayNumber) {
    int largerNumber;
    int tempSet[2];
    for (int i = 0; i < arrayNumber; ++i) {
        if (sumResult > largerNumber) {
            tempSet[i] = sumResult;
        }
    }
}


Your question reads

create a program that creates n arrays composed by 10 random integers....

  • But I can see just one int array of size 10. What you should be doing is allocate a 2D array of size arrayNumber X 10, call it say set.
  • Also you should allocate 1D array of size arrayNumber named say, sumArray to hold the sum of each array. so sumArray[i] will be sum of all the elements of the ith array in set.
  • Next you find the max element and its index(say maxIndex) in the sumArray
  • Print the Array at index maxIndex in set, which will be array having max sum.


To create arrays of arrays of ints an easy way is to do the following:

int **createArrayOfArrays(int number, int lengthOfEach)
{
    int **array_of_arrays = (int**) malloc(sizeof(int*) * number);
    int ix;
    for(ix = 0; ix < number; ix++)
    {
       array_of_arrays[ix] = (int*) malloc(sizeof(int) * lengthOfEach);
    }
    return array_of_arrays;
}

Don't forget to free all the memory at the end, you need to free each array and the array of arrays (one free per malloc).


n array of size 10 each can e made this way in c#

int n;
      n=  Convert.ToInt32(Console.ReadLine());


    int [,] mat=new int [n ,10];


    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 10; j++)
            mat[i,j] = 0;

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号