****NOTE: CANNOT BE TOO COMPLICATED, NOTHING MORE ADVANCED THAN FOR LOOPS, WHILE LOOPS, ETC ************ First, write a function deleteFromArray...

How It Works

Get an answer in three easy steps. Here's how it works...

Ask Your Question

1. Ask Your Question

Enter your programming question at the top of this page and click Get An Answer.

Pick Your Priority

2. Pick Your Priority

Tell us how quickly you want your programming question answered.

Get An Answer

3. Get An Answer

Connect with your programmer via online chat or telephone call.

Answer

Customer

****NOTE: CANNOT BE TOO COMPLICATED, NOTHING MORE ADVANCED THAN FOR LOOPS, WHILE LOOPS, ETC ************

First, write a function deleteFromArray which receives three arguments in its parameters: an array of integers; an offset indicating a position in the array; and an integer containing the size of the array. The function then deletes the value at the position indicated by the offset (by shifting all values to the right of the deleted value one offset to the left), and finally returns the new size of the array. Next, write a program to use this function. Particularly, the program first sets, declares, and initializes an array as follows: int myValues[10] = {0,1,1,2,3,5,8,13,21,34}; int arraySize = 10; Next, the program repeatedly displays the array to the user, asking whether they wish for a something to be deleted. If the user responds in affirmative, the program asks them for the offset in the array of the value to be deleted. It then calls your deleteFromArray function to carry out the actual deletion, receiving the new size of the array from the function's return value. If the user asks to delete from an offset that goes beyond the end of the array, an appropriate message is displayed stating that the deletion cannot be carried out. The program ends the first time that the user chooses not to delete a value from the array. Your main function may also call other functions to carry out some of its work. In other words, you may choose to encapsulate parts of the program's tasks in additional function, such as a function that displays the array on the console, and so on. Here is an example execution of the required program (input typed by the user is in green). The formatting of your program's output should match this example. Array: {0,1,1,2,3,5,8,13,21,34} Want to delete something? (y/n) y Offset to delete? 3 Array: {0,1,1,3,5,8,13,21,34} Want to delete something? (y/n) y Offset to delete? 7 Array: {0,1,1,3,5,8,13,34} Want to delete something? (y/n) y Offset to delete? 9 Sorry, can't delete that. Want to delete something? (y/n) n Goodbye! Carefully note the following in the example output above: The user first asks for deletion at offset 3, which refers to the value 2 in the array. Every value to the right of the 2 in the original array must be consequently copied one position to the left. For example, value 8 was at offset 6 before the first deletion (we deleted value 2, remember?), but appears at offset 5 (one left of its previous location) after the deletion. Also note that 10 elements were displayed before the first deletion, but 9 elements are displayed after it. To get this right, you need to keep track of the number of elements in the array, as well as the array itself. Also note that when the user tries to delete the item at offset 9 after the two deletions, when there were only 8 valid elements remaining in the array, the program declines to carry out the deletion.

Now, in a separate int main, write a program that asks the user for a positive integer N, and then uses that integer to compute and display the following four sums and products:

  1. The sum of integers 1 ... N inclusive.
  2. The sum of the powers of 2: 20 + 21 + 22 + ... + 2(N-1)
  3. The sum of the inverse of the powers of 2: 1/20 + 1/21 + 1/22 + ... + 1/2(N-1)
  4. The product of the integers 1 ... N inclusive Here is an example execution of the program with input 6 (the user inputs 6 in response to the request "Input N: ", but the program displays everything else). Note that your output should be formatted exactly as shown in the example below: Input N: 6 1 + 2 + 3 + 4 + 5 + 6 = 21 1 + 2 + 4 + 8 + 16 + 32 = 63 1 + 0.5 + 0.25 + 0.125 + 0.0625 + 0.03125 = 1.96875 1 * 2 * 3 * 4 * 5 * 6 = 720 Make sure you test your program! Your program should work for any positive integer (i.e., greater than 0)! Hint: Do this question in stages. Get the first sum working perfectly (tested and everything!) before going on to the second. You will cause yourself a lot of grief if you try to do this one all at once. And you can use the ideas you develop from earlier parts to solve later parts.
Last updated
Mario V.
Programmer

Hello and welcome to ExpertHelp.com! My name is Mario V. and I'm going to do everything in my power to answer your question to your full satisfaction!

We are sorry for the delay. Are you able to work with me on your question at 11:00 AM? If you aren't, please respond with a few times (including your timezone) that work best for you and we can connect then. 

May I know in what programming language do you need a solution?

Thank you,

Mario

Last updated
Customer

Hi Mario, 11 am doesn't work for me, but I am free from 9-10:30 am, 12:30-1:30 pm, and 3:30-5:15 pm. All these times would be in the central canada timezone (it is 7am right now). The solutions need to be in c++. I need this by tonight at 5:30 pm (my time) at the very latest, which is why I went with the urgent pay option. Thanks

Posted
Mario V.
Programmer

Hi,

Thank you for your response. No problem, I will prepare a solution for you and ask if I have any questions. Rest assured you will have a solution today. 

Best regards,

Mario

Posted
Customer

Okay great, please keep me updated.

Also, just in case it wasn't clear towards the bottom where it says "now, in a separate main..." this should solve for a separate N, and not draw on anything from the previous part. Thanks

Posted
Mario V.
Programmer

Hi,

I'm working on the solution. Do you mean we should have 2 programs/projects?

Thanks,

Mario

Posted
Customer

Hi basically yes, it is one question but with 2 separate parts in it

Posted
Mario V.
Programmer

 Hi, here is the second part:

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    double sum=0;
    double product=1;
    cout << "Input N: ";
    cin >> n;

    for (int i = 1; i <= n; i++)
        {
            cout << i << " + ";
            sum=sum+i;
        }
    cout << " = " << sum << "\n";

    sum=0;
    for (int i = 0; i <= n-1; i++)
    {
        cout << pow(2,i) << " + ";
        sum=sum+pow(2,i);
    }
    cout << " = " << sum << "\n";

    sum=0;
    for (int i = 0; i <= n-1; i++)
        {
            cout << 1/pow(2,i) << " + ";
            sum=sum+1/pow(2,i);
        }
    cout << " = " << sum << "\n";

    for (int i = 1; i <= n; i++)
        {
            cout << i << " * ";
            product=product*i;
        }
    cout << " = " << product << "\n";
    system("pause");
    return 0;
}

Please check if it's good. I will send you the first part soon.

Mario

Posted
Mario V.
Programmer

Hi,

Here is the first part of the problem. However, I'm afraid it still isn't finished because the array should be shortened with each deletion. I'm not sure how much time you have but I'm still working on it.

#include "stdafx.h"
#include <iostream>
using namespace std;

void deleteFromArray(int array[], int offset, int size){
    for (int i = offset; i < size; ++i)
        array[i] = array[i + 1];
}

int main()
{
    int myValues[10] = {0,1,1,2,3,5,8,13,21,34};
    char response;
    int offset;
    int size;
    do{
        size=sizeof(myValues)/sizeof(myValues[0]);
        cout << "Array: {";
        for (int i = 0; i < size-1; i++)
        {
            cout << myValues[i] << ",";
        }
        cout << myValues[size-1] << "}\n";
        cout << "Want to delete something? (y/n) ";
        cin >> response;
        if(response=='y'){
            cout << "Offset to delete? ";
            cin >> offset;
            if (offset >= 0 && offset < size){
                deleteFromArray(myValues, offset, size);
                myValues[9]=0;
            }
            else cout << "Sorry, can't delete that.";
        }
    }while (response=='y');
    cout << "Goodbye!" << "\n";
    system ("pause");
    return 0;

Best regards,

Mario

Posted
Customer

Hello, I just tried running the 2nd problem and it keeps coming up as an error with the return 0, what do I do?

Posted
Mario V.
Programmer

 Hi,

In what program are you trying to run the code? Please try to remove #include "stdafx.h" from the beginning of the code.

Thanks,

Mario

Posted
Customer

I'm running it in eclipse, and removing that still didn't work

 

Posted
Mario V.
Programmer

I ran it in Visual Studio but will check how it works in Eclipse now.

Posted
Customer

It's working now! But for the 1st question with the arrays, how do I get rid of the 0's at the end? I need each time to delete the 0 off the end please

Posted
Customer

Hi,

Also something else is that the "sorry cant delete that" for an element that does not exist is not popping up when it should be. how would i modify this?

Thanks

Posted
Mario V.
Programmer

The code needs to be modified in order to use vector instead of array or array should be created dynamically as standard arrays will always contain the same number of elements. That's what I was trying to do but without success so far.

Posted
Mario V.
Programmer

Sorry can't delete that will appear when index beyond the range of an array is entered, eg. if array have 10 elements you will get the message if you enter a number higher than 9 or lower than 0.

Posted
Mario V.
Programmer

Is there anything else I could help you with?

Posted
Customer

Yes that helped me, thank you!

Posted

quoteTestimonialsquote

About ExpertHelp

ExpertHelp is changing the way you connect with service professionals.

Whether you have a quick question while preparing your taxes, troubleshooting a computer problem, or need to hire an attorney, ExpertHelp is the most convenient and affordable way to connect with the right service professional to get the job done.

ExpertHelp has been in business since 2011, is an A+ Rated Better Business Bureau accredited member, and offers a 100% satisfaction guarantee on every question you ask!

More Programming Questions...

Ask Your Programming Question & Get An Answer Now!