Function and overload

Each program is necessary to alter the function and perform under overload

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
#define N 5
 
int main(int argc, char* argv[])
{
    int A[N] = { 0 };
 
    for (int t1 = 0; t1 < N; t1++)
    {
        A[t1] = rand() % 20 + 1;
        printf("%d ",A[t1]);
    }
 
    printf("\n\n");
 
    int max = 0;
    for (int i = 0; i < N; i++)
        max = (A[i] > A[max]) ? i : max;
 
    printf("maximum A[%d] = %d\n\n",max,A[max]);
 
    A[0] = A[max]; A[N-1] = A[max];
 
    for (int t2 = 0; t2 < N; t2++)
        printf("%d ",A[t2]);
 
    printf("\n");
 
    _getch();
 
    return 0;
}


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
#include <iostream>
using namespace std;
int main()
{
    int n,m,sum=0;
    cout<<"Input n - ";
    cin>>n;
    cout<<"Input m - ";
    cin>>m;
   int **a=new int*[n];  
   for(int i=0;i<n;i++)
    {
        a[i]=new int[m];
        for(int j=0;j<m;j++)
        {
            cout<<"a["<<i+1<<"]["<<j+1<<"] - ";
            cin>>a[i][j];
               if(a[i][j]<0)
                sum+=a[i][j];
        }
    }   
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
           if(((1+j)+(i*n))%3==0)
                a[i][j]=sum;
    for(int i=0;i<n;i++)
    {
            for(int j=0;j<m;j++)
            cout<<a[i][j]<<"\t";
        cout<<endl;
    }
    delete[] a;
    system("pause");
    return 0;
}

Last edited on
What is your question? I can only see one sentence and some code.
MatthewRock, the question is: How to remake these arrays to the function and then do function overloading?
Last edited on
Not really following what your problem is.

http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
What is all of this supposed to mean? Is this your assignment? If so, then have you tried solving it?

I'm not quite sure what you are supposed to do here. I could make any function from your code and overload it, ranging from readable solution to complete nonsense, and both would fit your description. Provide us with more information if you want us to help.
1 task: Given dimensional array A of size n elements. Calculate the max, replace a maximum of 1 and the last element of the array.
2 task: Given two-dimensional array A nxm. Calculate the sum of all negative elements and replace this amount every third element.
Help, please.

It is very necessary.
I'm assuming you need to combine these two into 1 program, you don't need function overloading for this, but since you asked for it I've used it.

I've written some code for you, I don't know if this is what you need, but it'll give you a start.

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> //Windows only, this is not portable, consider using something else.

void do_something(size_t N, int A[]) 
{
	int max = 0;
	for (int i = 0; i < N; i++)
		max = (A[i] > A[max]) ? i : max;
 
	std::cout << "maximum A[" << max << "] = " << A[max] << "\n\n";
 
	A[0] = A[max]; A[N-1] = A[max];
}

void do_something(size_t N, size_t M, int A[])
{
	int sum = 0;
	for (int i = 0; i < N*M; i++)
		sum += (A[i] < 0)? A[i] : 0;
 
	std::cout << "Sum of negatives: " << sum << "\n\n";
 
	for (int i = 0; i < N*M; i++)
		A[i] = (i%3 == 0)? sum : A[i];	
}

int main(int argc, char* argv[])
{
	int *A;
	size_t N;
	size_t M;
	std::cout << "Please specify the array width\n";
	do
	{
		std::cin >> N;
		if(N <= 0)
		{
			std::cout << "Array width must be a positive integer larger than 0\n";
			continue;
		}
	} while(false);

	std::cout << "Please specify the array depth\n";
	do
	{
		std::cin >> M;
		if(N <= 0)
		{
			std::cout << "Array depth must be a positive integer larger than 0\n";
			continue;
		}
	} while(false);
	A = (int*) calloc(N*M, sizeof(int));
	std::cout << '\n';
 
	for (int t1 = 0; t1 < M; t1++)
	{
		for (int t2 = 0; t2 < N; t2++)
		{
			A[t1*M+t2] = rand() % 40 - 20;  
			std::cout << A[t1*M+t2] << ' ';
		}
		std::cout << '\n';
	}
	std::cout << '\n';
 
	if(M==1)
	{
		do_something(N, A);
	}else{	
		do_something(N, M, A);
	}
 
	for (int t1 = 0; t1 < M; t1++)
	{
		for (int t2 = 0; t2 < N; t2++)
			std::cout << A[t1*M+t2] << ' ';

		std::cout << '\n';
	}
	getch();
        free(A);

	return 0;
Last edited on
Topic archived. No new replies allowed.