PROGRAMMING CODE REQUEST !!

Hello everybody, I want to ask you all guys if any of you can help me with this two questions. I need their codes !
PLEASE THIS IS AN EMERGENCY SITUATION FOR ME, HELP ME !
1. Given a set A={a, d, f, o, k, m}. Create one Symmetric and two more non Antisymmetric relations over the set A. Dynamic memory allocation and the memory safe releasing are provided.
2. Given a dynamic matrix, filled with random integers in the diapason of [513, 789]. Create a function that finds the matrix minimal and maximal elements and saves their sum digits in an array. The function returns a pointer to the matrix maximal element and copies the minimal element into a referenced variable of the main function. Memory safe releasing is provided.

for the second question I found this solution :
#include "stdafx.h"
#include < iostream >
#include < iomanip >
using namespace std;
void CreatingMatrix(int **,int &,int&);
void ShowingMatrix(int **,int &,int&);
void FillingArray(int**,int&,int&,int*);
void ShowingArray(int*);
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
int row=rand()%4+8;
int col=rand()%4+8;
int **M=new int *[row];
for(int i=0;i<row;i++)
{
*(M+i)=new int[col];
}
int *ESFAND=new int [4];
CreatingMatrix(M,row,col);
ShowingMatrix(M,row,col);
FillingArray(M,row,col,ESFAND);
ShowingArray(ESFAND);
for(int i=0;i<row;i++)

delete[]*(M+i);
delete[]M;
delete[]ESFAND;
return 0;
}
void CreatingMatrix(int**q,int&r,int&c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
*(*(q+i)+j)=rand()%277+513;
}
}
void ShowingMatrix(int**q,int&r,int&c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<setw(5)<<*(*(q+i)+j);
cout<<endl;
}
}
void FillingArray(int**M,int&row,int&col,int* ESFANDYAR)
{
int min=*(*(M)), max=*(*(M));
for ( int i=0;i<row;i++)
{
for ( int j=0;j<col;j++)
{
if ( *(*(M+i)+j)<(min))
{
min = *(*(M+i)+j);
}
if ( *(*(M+i)+j)>(max))
{
max = (*(*(M+i)+j));
}
}
}
cout << min << endl << max << endl;
int Sum=min + max;
int thousand = Sum/1000;
*(ESFANDYAR)=thousand;
int hundred = (Sum/100)%10;
*(ESFANDYAR+1)=hundred;
int ten = (Sum/10)%10;
*(ESFANDYAR+2)=ten;
int unit = Sum%10;
*(ESFANDYAR+3)=unit;
}
void ShowingArray(int* ESFAND)
{
for ( int i=0; i<4;i++)
{
cout << *(ESFAND+i)<<' ' ;
}

}

but something is missing " The function returns a pointer to the matrix maximal element " how can i complete this code ?
Last edited on
Please use code tags.
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
#include <iostream>
#include <iomanip>
using namespace std;
void CreatingMatrix(int **, int &, int&);
void ShowingMatrix(int **, int &, int&);
void FillingArray(int**, int&, int&, int*);
void ShowingArray(int*);
int main(int argc, char* argv[])
{
    srand(time(NULL));
    int row = rand() % 4 + 8;
    int col = rand() % 4 + 8;
    int **M = new int *[row];
    for(int i = 0; i < row; i++)
    {
        *(M + i) = new int[col];
    }
    int *ESFAND = new int [4];
    CreatingMatrix(M, row, col);
    ShowingMatrix(M, row, col);
    FillingArray(M, row, col, ESFAND);
    ShowingArray(ESFAND);
    for(int i = 0; i < row; i++)
    {
        delete[]*(M + i);
    }
    delete[]M;
    delete[]ESFAND;
    return 0;
}
void CreatingMatrix(int**q, int&r, int&c)
{
    for(int i = 0; i < r; i++)
    {
        for(int j = 0; j < c; j++)
        {
            *(*(q + i) + j) = rand() % 277 + 513;
        }
    }
}
void ShowingMatrix(int**q, int&r, int&c)
{
    for(int i = 0; i < r; i++)
    {
        for(int j = 0; j < c; j++)
        {
            cout << setw(5) << *(*(q + i) + j);
        }
        cout << endl;
    }
}
void FillingArray(int**M, int&row, int&col, int* ESFANDYAR)
{
    int min = *(*(M)), max = *(*(M));
    for ( int i = 0; i < row; i++)
    {
        for ( int j = 0; j < col; j++)
        {
            if ( *(*(M + i) + j) < (min))
            {
                min = *(*(M + i) + j);
            }
            if ( *(*(M + i) + j) > (max))
            {
                max = (*(*(M + i) + j));
            }
        }
    }
    cout << min << endl << max << endl;
    int Sum = min + max;
    int thousand = Sum / 1000;
    *(ESFANDYAR) = thousand;
    int hundred = (Sum / 100) % 10;
    *(ESFANDYAR + 1) = hundred;
    int ten = (Sum / 10) % 10;
    *(ESFANDYAR + 2) = ten;
    int unit = Sum % 10;
    *(ESFANDYAR + 3) = unit;
}
void ShowingArray(int* ESFAND)
{
    for ( int i = 0; i < 4; i++)
    {
        cout << *(ESFAND + i) << ' ' ;
    }
}

Error on what function.
It compiles fine for me(g++ 4.8)

  740  537  730  544  596  710  712  717  578
  631  624  784  708  692  575  751  726  617
  574  561  560  722  765  738  711  767  616
  744  517  760  717  744  632  657  623  563
  703  671  767  616  789  726  736  555  629
  521  516  565  750  577  738  520  786  713
  594  555  538  545  786  543  640  561  622
  759  554  733  657  592  614  759  695  613
516
789
1 3 0 5 
Sorry for not using code tags, didn't know that !
So, yes, you're right, it does work, but here's my question :
" What should I change to involve this sentence "The function returns a pointer to the matrix maximal element and copies the minimal element into a referenced variable of the main function " in my code ? "
ezand1128 wrote:
Sorry for not using code tags, didn't know that !
So, yes, you're right, it does work, but here's my question :
" What should I change to involve this sentence "The function returns a pointer to the matrix maximal element and copies the minimal element into a referenced variable of the main function " in my code ? "

You have to return a pointer to the maximum element from the matrix and pass the minimum element through an argument. IDK , which function it's referring to.
But this should the prototype of that function:
int *MatrixMaxMin(int **M,int xsize,int ysize,int &min);
return the maximum element and assign the minimum element to min.

Also you don't have to take the row and column sizes as references,pass it by value.

Hope that helps.
Last edited on
Got it man, everything is perfect, how about the first question ?
1. Given a set A={a, d, f, o, k, m}. Create one Symmetric and two more non Antisymmetric relations over the set A. Dynamic memory allocation and the memory safe releasing are provided.
I really do not have any idea how to do it,
can u solve it for me, I think it does not take your time more than 30 minutes, thank you so much.
Finding a relation on the given set ?
That looks like a mathematical problem to me to define a such a relation,I don't really get what you have to program here.
A relation is just a subset of the cartesian product of the given sets.
Here's symmetric relation:
http://latex.codecogs.com/gif.latex?Let%5C%20R%5Cto%20%5C%7B%28a%2Cd%29%2C%28d%2Ca%29%2C%28f%2Co%29%2C%28o%2Cf%29%29%29...%29%5C%7D
Here's an anit-symmetric relation:
http://latex.codecogs.com/gif.latex?Let%5C%20R%5Cto%20%5C%7B%28a%2Cd%29%2C%28f%2Ca%29%2C%28k%2Co%29...%29%5C%7D
here's another anti-symmetric one:
http://latex.codecogs.com/gif.latex?Let%5C%20R%5Cto%20%5C%7B%28o%2Cd%29%2C%28m%2Cf%29%29%5C%7D

It's pretty trivial to make such a relation in roster form,a computer can not really build a relation in set buider form easily .

Saw the photos, got the way,
but what I am supposed to do is that whenever I run my program I should see first the given set in my question " A={a, d, f, o, k, m} " then making the relation between them and print them out.
Here you go.
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
#include <iostream>
#include <deque>
#include <vector>

typedef std::pair<char,char> paircc;
typedef std::vector<paircc> relation;
typedef std::deque<char> set_type; //or a std::set if you wish

std::ostream& operator<<(std::ostream& out,const paircc& p)
{
    out << "{"<< p.first << "," << p.second << "}";
    return out;
}
std::ostream& operator<<(std::ostream& out,const relation& r)
{
    out << "{";
    for(const paircc& p : r)
        out << p << ",";
    out << "\b}";
    return out;
}
relation gen_sym_rel(const set_type& _set)
{
    relation rel;
    //sample algorithm,this is pretty trivial.
    size_t jshift = rand()%_set.size();
    for(size_t i=0,j=_set.size()-1;i<j;++i,--j)
    {
        rel.push_back( {_set[i],_set[(j+jshift)%_set.size()]} );
        rel.push_back( {_set[(j+jshift)%_set.size()],_set[i]} );
    }
    return rel;
}
relation gen_antisym_rel(const set_type& _set)
{
    //This is how it works,
    //Pick a random pair first say {a,d}
    //then in the next iteration the first element is 'd'
    //and we pick second as any other element but a or d.
    //continue till we reach the desired no. of elements in the relation
    relation rel;
    paircc prev{0,_set[rand()%_set.size()]};
    for(size_t i=0;i<_set.size();++i)
    {
        paircc p{prev.second,0};
        while( (p.second=_set[rand()%_set.size()]) == prev.first ||
              p.second == p.first);
        prev=p;
        rel.push_back(p);
    }
    return rel;
}
int main()
{
    srand(time(nullptr));
    set_type A{'a','d','f','o','k','m'};
    relation sym=gen_sym_rel(A),antisym1=gen_antisym_rel(A),
             antisym2=gen_antisym_rel(A);
    std::cout << "sym      : " << sym << std::endl;
    std::cout << "antisym1 : " << antisym1 << std::endl;
    std::cout << "antisym2 : " << antisym2 << std::endl;
    return 0;
}

You can try it here.
http://www.compileonline.com/compile_cpp11_online.php
Sample output:
sym      : {{a,o},{o,a},{d,f},{f,d},{f,d},{d,f}}
antisym1 : {{f,m},{m,k},{k,a},{a,d},{d,m},{m,a}}
antisym2 : {{o,m},{m,k},{k,d},{d,o},{o,m},{m,d}}

Try and understand why/how it works,and maybe make you own algorithm.
Please don't depend on others to do your homework. I've done it this time.
We could help you do it,help you fix it , or provide you a direction but we won't do it for you,that's like cheating.
So, next time try and do it yourself,show some code what you've done and we can help you with that.
Last edited on
Dude,I understand what you're saying but my problem is just something different, kinda complicated, it's the last project of the year for me, I really appreciate your kindness ,so ...,any way, I tried to run your last code but it didn't work !
my c++'s version is " Microsoft Visual C++ 2010 Express ", and also the website you suggested me, none of them succeed, has 4 errors !
1. Line 16, after " for(const paircc& p : r) " for the closed parenthesis it says " expected an expression ".
2. Line 27&28, the same error as line 16, " rel.push_back( {_set[i],_set[(j+jshift)%_set.size()]} ); " ,after " push_back " for the " { " it says " expected an expression ".
3. Line 40&43, " paircc prev{0,_set[rand()%_set.size()]}; " after " prev ", compiler says " { " " expected a " ; " ".
4.Last error in the main , " srand(time(nullptr)); "says identifier "time" is undefined.
So it's working on the compiler site you wrote but not in my c++, wanna know what's the reason behind it, Thanks buddy.
Last edited on
You couldn't compile it because it used C++11 ...
You would need g++ >=4.8.1 or MSVC 2013 to compile it.
Anyway here's the pre-c++11 version(or you can say c++03)
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
#include <iostream>
#include <deque>
#include <vector>
#include <cstdlib>

typedef std::pair<char,char> paircc;
typedef std::vector<paircc> relation;
typedef std::deque<char> set_type; //or a std::set if you wish

std::ostream& operator<<(std::ostream& out,const paircc& p)
{
    out << "{"<< p.first << "," << p.second << "}";
    return out;
}
std::ostream& operator<<(std::ostream& out,const relation& r)
{
    out << "{";
    for(size_t i=0;i<r.size();++i)
        out << r[i] << ",";
    out << "\b}";
    return out;
}
relation gen_sym_rel(const set_type& _set)
{
    relation rel;
    //sample algorithm,this is pretty trivial.
    size_t jshift = rand()%_set.size();//or jshift = 0
    for(size_t i=0,j=_set.size()-1;i<j;++i,--j)
    {
        rel.push_back( std::make_pair(_set[i],_set[(j+jshift)%_set.size()]) );
        rel.push_back( std::make_pair(_set[(j+jshift)%_set.size()],_set[i]) );
    }
    return rel;
}
relation gen_antisym_rel(const set_type& _set)
{
    //This is how it works,
    //Pick a random pair first say {a,d}
    //then in the next iteration the first element is 'd'
    //and we pick second as any other element but a or d.
    //continue till we reach the desired no. of elements in the relation
    relation rel;
    paircc prev(0,_set[rand()%_set.size()]);
    for(size_t i=0;i<_set.size();++i)
    {
        paircc p(prev.second,0);
        while( (p.second=_set[rand()%_set.size()]) == prev.first ||
              p.second == p.first);
        prev=p;
        rel.push_back(p);
    }
    return rel;
}
int main()
{
    srand(time(NULL));
    set_type A;
    A.push_back('a');
    A.push_back('d');
    A.push_back('f');
    A.push_back('o');
    A.push_back('k');
    A.push_back('m');
    relation sym=gen_sym_rel(A),antisym1=gen_antisym_rel(A),
             antisym2=gen_antisym_rel(A);
    std::cout << "sym      : " << sym << std::endl;
    std::cout << "antisym1 : " << antisym1 << std::endl;
    std::cout << "antisym2 : " << antisym2 << std::endl;
    return 0;
}

http://codepad.org/gSKetvA1
As you can say this is more verbose and the c++11 version was easier to read (IMHO :) ).
Last edited on
Topic archived. No new replies allowed.