help with these functions?

hi i am stumped with these functions i have attempted them but i am not getting the results that it should be
this is the driver
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
#include "mylist.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(void)
{
    const int ARRAY1 = 1;
    const int ARRAY2 = 2;
    cout << "const int ARRAY1 = 1;" << endl;
    cout << "const int ARRAY2 = 2;" << endl;
    MyList list;

    cout << "list.printArray(ARRAY1 ,20, false); " << endl;
    cout << "About to print the content of myIntArray, 20 values per line, in regular order" << endl
         << "Should show 1000 nines, 20 per line" << endl;
    list.printArray(ARRAY1, 20, false); //print content of myIntArray, 20 values per line, in regular order
    cout << "list.printArray(ARRAY2 ,20,false); " << endl;
    cout << "About to print the content of myCopyArray, 20 values per line, in regular order" << endl
         << "should show 50 zeros, 20 per line" << endl;
    list.printArray(ARRAY2, 20, false); //print content of myArrayCopy, 20 values per line, in regular order
    cout << "list.printArray(98, 20, false); " << endl;
    cout << "About to print the content of an array 98, 20 values per line, in regular order." << endl
         << "Problem is, there are only two options for the first parameter, 1 or 2" << endl
         << "Should default to myIntArray and show 1000 nines, 20 per line" << endl;
    list.printArray(98, 20, false); //print content of array 98, 20 values per line, in regular order  Problem is, there are only two options for first parameter, 1 or 2
    cout << "list.plainCopy(); " << endl;
    cout << "About to copy the first 50 elements of myIntArray to myCopyArray" << endl;
    list.plainCopy();
    cout << "list.printArray(ARRAY2 ,10,false);" << endl;
    list.printArray(ARRAY2, 10, false);
    cout << "About to print the content of myCopyArray, 10 values per line, in regular order" << endl;

    int multiple = 400;

    cout << " " << endl;
    int total = list.sum(multiple); //this is saying to sum myIntArray[0], myIntArray[400], myIntArray[800]
    cout << "The sum of values for positions that are multiples of " << multiple << " is: " << total << endl;
    multiple = 100;
    cout << "The sum of values for positions that are multiples of " << multiple << " is: " << list.sum(multiple) << endl; //sums positions [0], [100], [200], [300], [400], [500], [600], [700], [800],[900],
    list.reset();
    list.printArray(ARRAY1, 20, true);
    int numValuesToInsert = 5;

    bool result = list.promptAndInsert(ARRAY2, numValuesToInsert);
    if (result) //can also be written as if(result == true)
        cout << "Insert successful!" << endl;
    else
        cout << "Insert failed...try again."
             << "Cannot insert " << numValuesToInsert << " into array." << endl;
    numValuesToInsert = 400; //this should cause the function to return false if it is passed to array 2.
    if (list.promptAndInsert(ARRAY2, numValuesToInsert) == true)
        cout << "Insert successful!" << endl;
    else
        cout << "Insert failed...try again."
             << "Cannot insert " << numValuesToInsert << " into array." << endl;

    cout << "About to compare both arrays.  Each match should be shown with the value and location within the arrays" << endl;
    list.compareArrays();

    return 0;
}


this is the first function i cant get
11.) Create a member function called promptAndInsert. This function should prompt the user to enter a specific number of values from the keyboard and using a for loop, input each of them into either myIntArray or myArrayCopy. To achieve this, function promptAndInsert should have two parameters of type int. The first parameter determines which array should be inserted into. Again, 1 means myIntArray and 2 means myArrayCopy. If any value other than 1 or 2 is supplied, the function should use 1 as the default choice. The second parameter determines the number of values to be inserted. Ensure that this value is not less than 1 and not larger than the size of the selected array. If the user supplies a value that is outside the acceptable range, the function should simply return false. Let the function return true if everything is properly inserted. Thus, this function will have a bool return type

this is my code

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
bool MyList::promptAndInsert(int num,int howmany){



    if(num==1)

        for(int i =0;i<howmany;i++){

            cout<<"please enter your array into myIntArray:";
            cin>>myIntArray[i];

        }

    if(num==2)
        for(int i =0;i<howmany;i++){

            cout<<"please enter your array into myArrayCopy:";
            cin>>myArrayCopy[i];


        }
    else
        for(int i =0;i<howmany;i++){

            cout<<"please enter your array into my:";
            cin>>myIntArray[i];
           }
    if(howmany<1&&howmany>ARRAY_SIZE)
        return true;
     if(howmany<1&&howmany>ARRAY_SIZE2)
        return true;
    else
        return false;

}

this is the second one
12.) Create a member function called compareArrays that will compare the values in myArrayCopy with the first 50 elements of myIntArray. Also, each time it finds a match, it should print the word “found” followed by the value and index. For example, if position 9 of both arrays is found to contain the same value of 7, then it should print: found 7 at index 9. To achieve this (the hard way), declare an int array inside function compareArrays called tempArray and initialized it to all zeros. Array tempArray should be the same size as myArrayCopy. Now, using a for loop, compare the contents of myArrayCopy with myIntArray. Each time you find a match, place that value in tempArray at the same location where it was found in myArrayCopy and myIntArray. When you are finished, print the contents of tempArray. So, going back to the previous example, if position 9 of both arrays is found to contain the same value of 7, then it should insert into tempArray the value 7 at index 9. Now, since tempArray was initialized to all zeros, only those indices where a match was found will contain a non-zero value. Using this knowledge, you will print tempArray, searching for all locations whose value is non-zero and print the content and index. So, in our example, it would traverse tempArray and find that at index 9, the value stored is not zero. So, it will print found 7 at index 9.

this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void MyList :: compareArrays(){

int tempArray[ARRAY_SIZE2]={0};

for(int i = 0;i<ARRAY_SIZE2;i++){
    for(int j = 0;j<ARRAY_SIZE2;j++){
        for(int k =0;k<ARRAY_SIZE2;k++){
            if(myIntArray[i]==myArrayCopy[j]){
            myArrayCopy[j]=tempArray[k];
            }
            if(tempArray[k]!=0){
   cout<<"found"<<tempArray[k]<<" at index"<<k;
            }

}
}
}
}

i have gotten the other functions exept these 2


Last edited on
PromptAndInsert() needs to ensure that howmany is valid before it tries to insert the items.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool MyList::promptAndInsert(int num,int howmany){

    if (howmany < 1 || howmany > ARRAY_SIZE) {
        return false;
    }

    if(num!= 2) {               // num == 1 or the default
        for(int i =0;i<howmany;i++){
            cout<<"please enter your array into myIntArray:";
            cin>>myIntArray[i];
        }
    } else {                    // num == 2
        for(int i =0;i<howmany;i++){
            cout<<"please enter your array into myArrayCopy:";
            cin>>myArrayCopy[i];
        }
    }
    return true;
}


To achieve this (the hard way),
This is basically saying "here is one way and you should not use it. Just go through the arrays and print out the matches. There's no need to store in a separate array.
1
2
3
4
5
6
7
8
void MyList::compareArrays(){

    for(int i = 0;i<50;i++){
        if(myIntArray[i]==myArrayCopy[i]){
            cout<<"found"<<myIntArray[i]<<" at index"<<i;
        }
    }
}

Since all you said was that you aren't getting the results you expect, I can only guess what the problem might be.

Based on a quick perusal of your code, I see that line 14 of your second snippet should be "else if". Otherwise, num==1 will trigger lines 7 - 12 AND lines 23 - 27.

Topic archived. No new replies allowed.