Jump to content

I NEED HELP WITH MY C++ HOMEWORK

Alexperson123

I am currently in my first C++ class in college. I am struggling to understand what my teacher wants me to do with my assignment and I was hoping someone could help me break it down. Our current chapter is covering pointers and my last one was on arrays, but i think it also wants me to use file streaming but I was completely lost in that chapter. Below here is exactly what was written for my assignment:

 

Write a function that accepts an int array and the arrays’ size arguments. The function should:


1. create a new array that is twice the size of the argument array.
2. copy the contents of the argument array to the new array and initialize the unused elements
of the second array with 0.
3. return a pointer to the new array.


Demonstrate the function by using it in a main program that reads an integer N (that is not more
than 50) from standard input and then reads N integers from a file named data into an array (DO
NOT use an absolute path to the data file). The program then passes that array to your array
expander function, and displays the values of the new expanded array, one value per line.
You may assume that the file data has at least N values. If the integer read in from standard input
exceeds 50 or is less than 0 the program terminates with an appropriate message.

 

Link to comment
Share on other sites

Link to post
Share on other sites

We won't do homework for you, we can help you but you need to show us where you are getting stuck.

 

Are you having issues understanding what function to create?  If so, what do you think it currently is asking to do.  I mean at it's core the function they are asking you to create is a pretty basic function, so we really do need to know where you are getting stuck on or where your understanding level is.

 

Or are you having issues with the later part of the wording?  If you have already attempted writing the code, post it here and we can start picking it apart and telling you where it's going wrong and why it's going wrong.

 

In essence the second part of the assignment is to use your expanding array function.

So the user puts in like 20 as an example into the program, and it's assigned to N

Create an array of N (20) elements.

Then you read N (20) integers from the file called data

You then pass the array into your function, along with the current size of the array.

 

The first paragraph is actually quite self explanatory so if you are struggling with that bit we need to know where you are in regards to knowledge/progress on attempting it.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Just checking that you want C++ and not just C. Most universities that I know of don't bother with C++ and just go for C and this defiantly sounds like a C problem since they're asking you to work with raw pointers and arrays which is very uncommon in C++.

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Alexperson123 said:

but i think it also wants me to use file streaming but I was completely lost in that chapter.

 

Yes.  This code example shows the basic usage of file streams.

int n;
std::in >> n;

std::fstream fs ("data", std::fstream::in);
for (int i = 0; i < n; i++) {
	int tmp;
    fs >> tmp;
    std::out << tmp;
}
fs.close();

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, lochlanna said:

Just checking that you want C++ and not just C. Most universities that I know of don't bother with C++ and just go for C and this defiantly sounds like a C problem since they're asking you to work with raw pointers and arrays which is very uncommon in C++.

Yes this is a C++ class. I've heard a lot online that my teacher is teaching everything oddly

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/21/2023 at 12:53 AM, wanderingfool2 said:

We won't do homework for you, we can help you but you need to show us where you are getting stuck.

 

Are you having issues understanding what function to create?  If so, what do you think it currently is asking to do.  I mean at it's core the function they are asking you to create is a pretty basic function, so we really do need to know where you are getting stuck on or where your understanding level is.

 

Or are you having issues with the later part of the wording?  If you have already attempted writing the code, post it here and we can start picking it apart and telling you where it's going wrong and why it's going wrong.

 

In essence the second part of the assignment is to use your expanding array function.

So the user puts in like 20 as an example into the program, and it's assigned to N

Create an array of N (20) elements.

Then you read N (20) integers from the file called data

You then pass the array into your function, along with the current size of the array.

 

The first paragraph is actually quite self explanatory so if you are struggling with that bit we need to know where you are in regards to knowledge/progress on attempting it.

I'm just having a hard time understanding what the question is. I was hoping you guys could help pretty much rewrite the problem in a way that makes a bit more sense to me I guess

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Alexperson123 said:

I'm just having a hard time understanding what the question is. I was hoping you guys could help pretty much rewrite the problem in a way that makes a bit more sense to me I guess

The basic idea is that you start with an array, that contains a certain amount of numbers, for example

[1, 2, 3]

 

Your program is supposed to double the length of the array, and fill the rest with zeros. So the array above, which contains 3 elements, should become 6 elements in size and the last three should be zeros.

[1, 2, 3, 0, 0, 0]

 

That's really all there is to it. The rest of the description tells you how you're supposed to initialize that array. When you start the program, it's supposed to ask for a number, so e.g. something like

Give me a number between 0–50:
4

 

In the example above, assume the user entered 4, and pressed enter. The program is now supposed to create an array with a length of four, and read values from a "data" file into it. So for example lets say the data file looks something like this:

1 8 9 4 6 3 4 5 2 6 8 3 9 6 1 4 8 3 7 …

 

Since you entered 4, the program should initialize its array with the first four numbers from the data file, so it should look like

[1, 8, 9, 4]

 

It should then double it and fill the rest with zeros

[1, 8, 9, 4, 0, 0, 0, 0]

 

The program should then print the resulting array, one value per, line, so the whole output (in this example) should be

Give me a number between 0–50:
4<enter>
1
8
9
4
0
0
0
0

 

The rest of the instructions deals with error handling. Your program should abort if the value you enter is less than zero, or more than 50, so for example

Give me a number between 0–50:
-10<enter>

Error, number must be between 0 and 50
<exit>

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/24/2023 at 12:32 AM, Eigenvektor said:

The basic idea is that you start with an array, that contains a certain amount of numbers, for example

[1, 2, 3]

 

Your program is supposed to double the length of the array, and fill the rest with zeros. So the array above, which contains 3 elements, should become 6 elements in size and the last three should be zeros.

[1, 2, 3, 0, 0, 0]

 

That's really all there is to it. The rest of the description tells you how you're supposed to initialize that array. When you start the program, it's supposed to ask for a number, so e.g. something like

Give me a number between 0–50:
4

 

In the example above, assume the user entered 4, and pressed enter. The program is now supposed to create an array with a length of four, and read values from a "data" file into it. So for example lets say the data file looks something like this:

1 8 9 4 6 3 4 5 2 6 8 3 9 6 1 4 8 3 7 …

 

Since you entered 4, the program should initialize its array with the first four numbers from the data file, so it should look like

[1, 8, 9, 4]

 

It should then double it and fill the rest with zeros

[1, 8, 9, 4, 0, 0, 0, 0]

 

The program should then print the resulting array, one value per, line, so the whole output (in this example) should be

Give me a number between 0–50:
4<enter>
1
8
9
4
0
0
0
0

 

The rest of the instructions deals with error handling. Your program should abort if the value you enter is less than zero, or more than 50, so for example

Give me a number between 0–50:
-10<enter>

Error, number must be between 0 and 50
<exit>

 

Thank you, This has given me a start. I'm just now struggling with the pointers

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×