Question

Can someone help me solve this problem? Everything works but I keep getting an error "expression...

Can someone help me solve this problem? Everything works but I keep getting an error "expression must have a constant value". Its the Extended Euclidean Algorithm

#include <iostream>
using namespace std;
#include<vector>


void TwoLargest(int a[], int x)
{
   int largeOne = a[0];
   int largeTwo = a[0];

   for (int i = 1; i < x; i++)
   {
       if (a[i] > largeOne)
       {
           largeTwo = largeOne;
           largeOne = a[i];
       }
       else if (largeTwo < a[i])
       {
           largeTwo = a[i];
       }
   }
   cout << "\nThe largest two numbers are: " << largeOne << " , " << largeTwo;

}

int main()
{
  
   vector<int> v;
   int x; //size of array or list

   cout << "\nList Size: ";
   cin >> x;

   int arr[x];//sets the array to the size user wants list to be

   cout << "\nNumbers in the list: ";
   for (int i = 0; i < x; i++)
   {
       cin >> arr[i];
   }
   TwoLargest(arr, sizeof(arr) / sizeof(arr[0]));

   system("pause");

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
I f you are using a variable to set size of array. then you must use dynamic array using

#include <iostream>

using namespace std;

#include<vector>


void TwoLargest(int a[], int x) {
    int largeOne = a[0];
    int largeTwo = a[0];

    for (int i = 1; i < x; i++) {
        if (a[i] > largeOne) {
            largeTwo = largeOne;
            largeOne = a[i];
        } else if (largeTwo < a[i]) {
            largeTwo = a[i];
        }
    }
    cout << "\nThe largest two numbers are: " << largeOne << " , " << largeTwo;

}

int main() {

    vector<int> v;
    int x; //size of array or list

    cout << "\nList Size: ";
    cin >> x;

    int *arr = new int[x];//sets the array to the size user wants list to be

    cout << "\nNumbers in the list: ";
    for (int i = 0; i < x; i++) {
        cin >> arr[i];
    }
    TwoLargest(arr, x);

    system("pause");

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Can someone help me solve this problem? Everything works but I keep getting an error "expression...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...

    Can someone explain how this C++ program runs? A line by line explanation/commentation would be great, as well as the purpose of the program and functions/classes involved. #include <iostream> #include <vector> using namespace std; // template function vector<int> removeEvenIndexedVals(vector<int> vec); // main int main() {        static const int arr[] = { 2,5,7,9,1,3,6 };        vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));        // call function        vec = removeEvenIndexedVals(vec);        // print        cout << "Displaying the Vector Elements:"...

  • C++ Code error help. I am getting the error: "expression must have a constant value, the...

    C++ Code error help. I am getting the error: "expression must have a constant value, the value of parameter 'n' ( declared at line 7) cannot be used as a constant" I am also getting this error at lines 65 and 66 with m and n. I do not know how to fix this. Here is my code and ive marked where the errors were with lines: #include<iostream> using namespace std; // Function to allocate memory to blocks as per...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

  • c++ Please help! i keep getting an error message. I think my main problem is not...

    c++ Please help! i keep getting an error message. I think my main problem is not understanding the circle calculations function and how both the area and the circumference is returned from the function. I know & needs to be used, but I am unsure how to use it. Copy the following code and run it. You should break it into the following 3 functions getValidinput - which asks the user to enter the radius and then make sure that...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

    use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT