15,846,211 members

See more:

There are inputs that consists of 1000 lines.Every line has an array that consists of 30 numbers.first 15 numbers are betweeen 1-10 and last 15 numbers are their squares.the code will read the numbers from the left and if we have the same number in right these numbers will be replaced.Then it will continue to read and write and when left index equals to right index the code will stop.

**What I have tried:**

i tried to use 2 loops first one was for the first half and the second one was for the second half but i couldn't continue.

i tried to use 2 loops first one was for the first half and the second one was for the second half but i couldn't continue.

Comments

The documentation for fgets[^] includes sample code that shows how to open, read, and close a file and that is how you can read each line in the file. The function strtok[^] can be used to peel the numbers out of the line of text and atoi[^] can be used to convert each token string to an integer which you will probably want to store in an array. It is then up to you to perform the logic on those numbers.

Permalink

Share this answer

Replaced by what ?Quote:the code will read the numbers from the left and if we have the same number in right these numbers will be replaced.

You have to search the number of a set (e.g. the ones on the left) in the other set (e.g. the ones on the right). The straightforward, brute force, approach uses two loops:

pseudocode

for i = 1 to 15 for j = 16 to 30 if n[i] == n[j] replace

Permalink

Share this answer

Comments

Alperen E
25-Jan-23 5:49am

if we have the same numbers both in the first and in the last 15 these numbers will be replaced.i just thought about that and it felt weird to me.there must be a mistake in this question

Quote:if we have the same numbers both in the first and in the last 15 these numbers will be replaced.i just thought about that and it felt weird to me.there must be a mistake in this question

Not really: there are three numbers which can appear on both sides: 1, 4, and 9 because they are squares between 1 and 10

So the simplest efficient way to do this is to look for those three values on the right, and if you find one, look for a matching 1, 2, or 3 on the left.

If you find one, remove them.

If the indexes of the match are the same, stop processing.

Permalink

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

It is very difficult to give an answer on something as broad as your question is. See this to get a better idea on how to improve on your questions - https://www.codeproject.com/Questions/297604/How-to-ask-a-good-question

"if we have the same number in right these numbers will be replaced."Replaced by what?