Assignment with arrays

Hello, I need help with this assignment and I have zero ideas what to do and how to even start. Could anyone help at all.
(1) Function Algorithms for Array Element Processing

Write a modular program that processes array elements using specialized function algorithms. In addition to main, the program should have at least six functions with four arrays provided to process stored data. You may use the partial sample array set-up with test data using an initialization list below ... or develop your own programming interface and algorithm...

//list function prototypes here

int main ()
{
int arrayData [10][4];
int array1 [20];
int array2 [20];
int array3 [4] = { 11, 13, 15, 17 };


// your code here.... function calls etc.

}

//list all function definitions below main

a. Write the prototype and definition of a function setArray that when called initializes any one-dimensional array of type int to 0;

b. Write the prototype and definition of a function fillArray that when called prompts the user to input 20 numbers and stores the numbers into array1.

c. Write the prototype and definition of a function doubleArray that when called initializes the elements of array2 to two times the corresponding elements in array1. Make sure that you prevent the function from modifying the elements of array1. (Hint: use a const parameter)

d. Write the prototype and definition of a function copyTwoArrays that when called stores array1 elements into the first five rows of arrayData and array2 elements into the last five rows of arrayData. Make sure that you prevent the function from modifying the elements of array1 and array2. (Hint: use a const parameter)

e. Write the prototypes and definitions for two functions print1DArray and print2DArray that when called prints the elements in any one or two dimensional array of type int.

f. Write C++ expressions in main that call each of the above functions in part a through e.

Your program should test all function algorithms (a through e) with calls in main and should provide appropriate display of the final contents of array1, array2 and arrayData in good format.

If you are not able to complete all function algorithms, credit will be provided for algorithms completed that meets specification only if your program compiles.
a is silly, since you can do that directly. but ok, lets use it as an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void setArray(int *arr, unsigned int size); //prototype. 

void setArray(int *arr, unsigned int size)
{
    memset(arr,0, size*sizeof(int));
}

int main()
{
    const int arrsize = 100;
    int arr[arrsize];
    setArray(arr, arrsize);
}



c: did you know that shifting multiples by 2, or divides if you go the other way?
Last edited on
your own programming interface and algorithm


Maybe :) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <algorithm>
#include <iostream>

template <size_t N>
void setArray(int (&data)[N], int d = 0);

template<size_t N>
void fillArray(int (&data)[N]);

template<size_t N>
void doubleArray(const int (&datain)[N], int (&dataout)[N]);

template<size_t N>
void print1DArray(const int (&data)[N]);

template<size_t N, size_t M>
void print2DArray(const int(&data)[N][M]);

template <size_t N, size_t O, size_t P>
void copyTwoArrays(const int(&arr1)[N], const int(&arr2)[N], int(&out)[O][P]);

int main() {
	int arrayData[10][4];
	int array1[20];
	int array2[20];
	const int array3[4] { 11, 13, 15, 17 };

	print1DArray(array3);

	// a
	setArray(array2);

	// b
	fillArray(array1);
	print1DArray(array1);

	// c
	doubleArray(array1, array2);
	print1DArray(array2);

	// d
	copyTwoArrays(array1, array2, arrayData);
	print2DArray(arrayData);
}

template <size_t N>
void setArray(int (&data)[N], int d) {
	std::ranges::fill(data, d);
}

template<size_t N>
void fillArray(int (&data)[N]) {
	std::cout << "Please enter " << N << " integers: ";
	for (auto& n : data)
		std::cin >> n;
}

template<size_t N>
void print1DArray(const int (&data)[N]){
	for (const auto& n : data)
		std::cout << n << ' ';

	std::cout << '\n';
}

template<size_t N>
void doubleArray(const int (&datain)[N], int (&dataout)[N]) {
	for (size_t i {}; i < N; ++i)
		dataout[i] = datain[i] << 1;
}

template <size_t N, size_t O, size_t P>
void copyTwoArrays(const int(&arr1)[N], const int(&arr2)[N], int(&out)[O][P]) {
	static_assert(N * 2 == O * P);

	auto p {&out[0][0]};

	for (const auto& n : arr1)
		*p++ = n;

	for (const auto& n : arr2)
		*p++ = n;
}

template<size_t N, size_t M>
void print2DArray(const int(&data)[N][M]) {
	for (const auto& r : data)
		print1DArray(r);
}

Last edited on
Registered users can post here. Sign in or register to post.