help please with array

what's wrong with this code ? on the output " rating 6,8,10"

also does array1[array[i]]++;
means that it gives the value let's say i=1;
it gives array1 the value 2 on the second element ? then what does ++ do exactly ?

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
  #include<iostream>
#include<iomanip>
using namespace std;

int main(){
	
const int size1=40;
const int size2=11;

const	int array[size1]={1,2,6,4,8,5,9,7,8,10,1,6,3,8,6,10,3,8,2,7,6,5,7,6,8,6,7,5,6,6,5,6,7,5,6,4,8,6,8,10};
	int array1[size2];
	
		for (int i = 0; i<size1; i++){
			array1[array[i]]++;
		}
		
		cout<<"rating"<<setw(20)<<"freq"<<endl;
		
		for (int r=1; r<size2; r++){
			cout<<setw(9)<<r<<setw(20)<<array1[r]<<endl;
		}
	

	cin.get();
	cin.get();
	return 0;
}

what I meant is in line 14 , I really need help guys ,
please help guys ..
This code counts the number of times each element from 1 to 10 is encountered in array. This count is stored in array1.

There is a logical error. You need to initialize the count of all indices to zero initially.

int array1[size2] = {0};

Also, to answer your question, the ++ operator is a unary operator (that takes one input parameter) that simply increments the value of the variable by one.

Hence array1[ array[i] ]++, simply increments the count for the element array[i].
Last edited on
Thank you , I wanted to ask , how does it count from 0 to 39 and store it correctly ? for example how does the program knows that there's 2 student voted 1 ?

does the program uses ++ to store array[0] value to array1[1] ? that's why we put ++ and started the other loop from 1 ?

how does array1 stores 40 values even though we our size was 11 ?
array1 does not store 40 values. Its stores the frequency of each element from 1 to 10.

array1[i] = number of times i is encountered in original array.

If you add up all the array1[i]'s from 1 to 10, then you will get 40.

I suggest you go through the tutorial on this website to brush up on your basics.
I'm using a book to self study ,and this question is from another book that some of it got covered on the slides.

What I wanted is : first let's forget about " ++ "

let's say
I used this :
array1[array[3]];

it'll start from 0 to 39 since the loop stops " i<size1 " which is 40"

so he'll bring array[3] value which is 4 so we'll get array1[4] ?

or it'll just store the value 4 , but how will it determine which place ? for example stores it in array1[1] or array[2] ... etc

" array1 does not store 40 values. Its stores the frequency of each element from 1 to 10. "
that's why I couldn't understand this , how could the program determine that who rated 5 are actually 5 student and then store it on array1[5] ?
You are right that when you access array1[ array[3] ], you are accessing array1[4].

Now this means that since it has encountered a '4', the counter at that position (array1[4]) increments by 1.
Thank you sir , you helped a lot .

I think I got it , so it does depends on how many times the elements of array have showed , so array[1] for example have shown twice so the first time array1[1] changed from 0 to 1 and the second one the it changed from 1 to 2.

Things are much clear now.. thanks a lot sir ^^
Last edited on
I'm glad it helped you understand :)
Topic archived. No new replies allowed.