C++ Mode calculator problem (noob having problems)

In summary, the individual is a first-time programmer learning C++ in an engineering class. They have successfully written a program to find the mean, median, and mode of ten random numbers, but are now encountering errors when trying to set up a second array to count the number of occurrences of the first array. They have googled the errors, but are still unsure of their meaning. The individual shares their code snippet and the specific errors they are getting. However, they later realize their mistake of declaring the "int fakearray" variable twice. They thank anyone who took the time to help and apologize for not using the proper formatting tags.
  • #1
USN2ENG
108
0

Homework Statement


Just as a background this is my first time ever learning to program. I am in an engineering C++ class. I had to write a program that took in ten random numbers and then found the mean median and mode. I got the first two and was able to figure out how to sort correctly. But now I am trying to set up a second array to count the number occurrences of the first array and I am getting a couple errors. I googled the errors but I am still not sure what it exactly means.


The Attempt at a Solution


My code snippet (if you need more, just let me know, I just didnt want to fill the whole page up):

int fakearray[10]={1,1,1,1,1,1,1,1,1,1};
int temp = 0;

for(int y=0; y < 10; y++)
{
temp=Bubble[y];
for(int x=0; x<10;x++){
if(Bubble[x]==temp){
int fakearray[y]= fakearray[y]++; //This is where all the errors are pointing
}
}
}

The errors I am getting:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2440: 'initializing' : cannot convert from 'int' to 'int []'
There are no conversions to array types, although there are conversions to references or pointers to arrays

Thanks to anyone that can help point me to the right path!
 
Physics news on Phys.org
  • #2
Haha, nevermind, I just realized I had int fakearray twice...well thanks if you looked!
 
  • #3
Put your programs in [noparse]
Code:
[/noparse] tags to make them more readable.
 
  • #4
Thanks! Will do next time, didn't know that was an option.
 
  • #5



Hello there,

First of all, congratulations on taking on the challenge of learning a new programming language. It can be tough at first, but with practice and persistence, you will become more comfortable with it.

Now, let's take a look at the errors you are getting. The first one, "error C2057: expected constant expression" is indicating that you are trying to create an array with a size that is not a constant value. In your code, you have declared an array called "fakearray" with a size of 10, but then in the for loop, you are trying to declare another array with the same name and size. This is causing a conflict because you cannot have two arrays with the same name. Instead, you should use a different variable name for your second array, such as "count".

The second error, "error C2466: cannot allocate an array of constant size 0" is a result of the first error. Since you are trying to create an array with a size that is not a constant value, the compiler is unable to allocate memory for it and therefore, it has a size of 0.

The third error, "error C2440: 'initializing' : cannot convert from 'int' to 'int []'" is also related to the first error. In your code, you are attempting to assign a value to the array "fakearray[y]", but since it is not a constant value, the compiler does not know how much memory to allocate for it. Instead, you should use the count variable to keep track of the number of occurrences of each number.

I would also suggest using a nested for loop to iterate through both arrays and count the occurrences of each number. Something like this:

int count[10] = {0}; //initialize the count array with all 0s

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (Bubble[j] == Bubble) {
count++; //increment the count for that number
}
}
}

I hope this helps to clarify the errors you are getting and provides a possible solution to your problem. Keep practicing and don't be afraid to ask for help when needed. Good luck with your programming journey!
 

FAQ: C++ Mode calculator problem (noob having problems)

What is the C++ Mode calculator problem?

The C++ Mode calculator problem is a common programming challenge that involves creating a calculator program in C++ that can perform basic arithmetic operations and switch between different modes, such as addition, subtraction, multiplication, and division.

What are some common problems that beginners encounter when trying to solve the C++ Mode calculator problem?

Beginners often struggle with understanding the syntax and logic behind creating a functional calculator program in C++. They may also have difficulty implementing the different modes and handling user input.

How can I approach solving the C++ Mode calculator problem?

It is important to break down the problem into smaller, manageable steps. Start by creating a basic calculator program that can perform one operation, such as addition. Then, add in the other modes and implement error handling for user input. It can also be helpful to reference online resources and seek assistance from experienced programmers.

What are some tips for debugging my C++ Mode calculator program?

When debugging, it is important to go through your code line by line and check for any errors or inconsistencies. You can also use print statements or a debugger to track the flow of your program and identify any issues. It can also be helpful to have someone else review your code for a fresh perspective.

Are there any specific techniques or best practices for improving my C++ Mode calculator program?

One useful technique is to use functions to break down your code into smaller, reusable blocks. This can make your code more organized and easier to debug. It is also important to add comments and documentation to your code to make it easier to understand and maintain in the future.

Similar threads

Replies
2
Views
1K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
23
Views
7K
Back
Top