Array

Hello, I am a bit lost. I was given to try and do this program but I am not really familiar with arrays so I am in need of big help. At the moment I am trying to make a program for this task but I don’t know how to start. I would really appreciate anyones help. Thank you!

Task:
There are n (1<=n<=100) lights in the park. We know which lights where working and which ones weren’t on the first night. During other nights to identify if lights where working we use these rules.
1. The lights won’t work if the previous night both of his neighbours where working.
2. The lights will work if the previous night one of his neighbours was working and the other wasn’t.
3. If those two rules don’t apply the lights will work if it’s an even night and if the night isn’t even it won’t work.

I need to make a program which would identify which lights are going to work and which ones won’t after k (1<=k<=100) nights and find which day there was the most and least working lights. If there are numerous of nights with most and least working lights we need to find the night which number is the smallest.

The first numbers represent the number of lights (n) and the other one the number of nights (k).
The other numbers are the lights status. One means the lights where working the first night and zero means that the lights weren’t working the first night.

Initial data:
6 2
0 1 0 1 1 1

Resulst are:
1 1 0 1 0 1
The night with the most working lights: 1
The night with the least working lights: 0

Program:

#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>

using namespace std;
void Reading (int &n, int &k, int s[]);

int main (){
int n, // number of lights
k, // number of nights
s[100]; // array
Reading (n, k, s);



return 0;
}
//--------------
void Reading (int &n, int &k, int s[]){
ifstream fd("initialdata.txt");

fd >> n >> k;

for (int i=1; i<=n; i++){
fd>> s[i];


}
fd.close();

}
//------------------
Last edited on
I am not really familiar with arrays

Arrays are a fundamental tool of C/C++. Learn C++ has several lessons on arrays you might want to spend some time reading. The starting lesson is:

https://www.learncpp.com/cpp-tutorial/arrays-part-i/

The chapter also deals with dynamic arrays, C-style strings and C++ containers. You should read those parts of chapter 11 as well.

Also....
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
This would be easier if you used std::vector instead of array.

https://cplusplus.com/reference/vector/vector/

> but I don’t know how to start.
The same way all programs start.

Lot's of doodling on paper.

subscript  0   1   2   3   4   5
         +---+---+---+---+---+---+
array    | 0 | 1 | 0 | 1 | 1 | 1 |
         +---+---+---+---+---+---+

         +---+---+---+---+---+---+
new      | ? | ? | ? | ? | ? | ? |
         +---+---+---+---+---+---+

If n is 2 say, then the neighbours are at array[n-1] and array[n+1].


> 1. The lights won’t work if the previous night both of his neighbours where working.
An important first question being "what happens at the first and last position, which only have one neighbour".
- does it wrap around so that light 1 and light 6 are neighbours?
- do the ends have imaginary neighbours that are always on (or off)?

I need to make a program which would identify which lights are going to work and which ones won’t after k (1<=k<=100) nights and find which day there was the most and least working lights. If there are numerous of nights with most and least working lights we need to find the night which number is the smallest.

Also, don't try to solve the entire problem in a single edit.
Figuring out how to break the problem down into manageable steps is a key skill.
- just print all the generations over k nights.
- add to that how many lights were lit
- add to that when the min/max occurred.


Hello. I understand what you are trying to do, but (sorry) this program is just very annoying. I guess that they could give you another exercise - this one is not really useful and really confusing. However, as someone said above, it could be better to use Vector instead of Array which has a defined memory space. Vectors can be modified according to your will. At the same time, you said that you have only 100 lights - no more. I can understand that you want to use Array. I wish you the best ++
Thank you all for your help! I think I got the hang of it and actually did most of it.
Topic archived. No new replies allowed.