Please help!!!

Removing elements
Time Memory Input Output
1 with 64 Mb standard input standard output
The number is undesirable in a series of integers if it divides the total number of elements (for example, in a length of 10, there are undesired elements that share number 10, which are 1, 2, 5, and 10). It is necessary to find all undesired items in a row and remove them. After that the number of elements can be changed and some other elements may become undesirable. The procedure is repeated until a sequence is obtained without undesirable elements. Write a program that specifies the set of remaining elements for the given string, after removing unwanted.

Entrance
The number is entered from the standard input
n
(
1

n

50000
), and then i
n
elements of the range from the range from 1 to 100.

Exit
On the standard output, print an entire number that represents the sum of the remaining elemenates in a row, after successively removing all undesired elements.

Example
Entrance
10
1
2
3
4
5
6
7
8
9
10
Exit
24
First, the elements 1, 2, 5, and 10 that divide the length 10 are removed, then the elements 3 and 6 dividing the length 6 are removed and eventually the element 4 is removed, so that the elements 7, 8 and 9 remain the sum of 24.
Hello luka34,

Welcome to the forum.

Nice explanation, but I have no idea what you want, so may answer is 42. See https://www.youtube.com/watch?v=aboZctrHfK8 to help explain.

Not sure if this applies?

As keskiverto once said:
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
Or say that you do not understand the instructions or how to figure them out.

Post something to work with then we have something to work with.

Hope that helps,

Andy
/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

using namespace std;

int main()
{
int i=0,s=0,a[i],n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]%n==0)
{
n--;
a[i]=0;

}
s+=a[i];

}
printf("%d",s);

return 0;
}

I was trying this,but it seems it doesnt work.I got few okay answers but mostly run time errors.
I would be glad if i get help.
Thanks
Hello luka34,

I am surprised it even worked at all. If it does I would say that you need to update your compiler.

The line using namespace std;. What in your program is in the standard name space? When you can answer that you will understand that this line is not needed in a C program.

"int i = 0" great you initialized your variable, but then you try "a[i]" which is not allowed even in C. "i" must be defined as a constant or an actual number. Also what you are trying to do is create a zero length array because you defined "i" and set it to zero first. Again this is not allowed. Even if it is one the array needs to have a length.

In the for loop you use "a[i]" even if it does run it will not work because "a" is a zero length array.

The last error I had is with the "scanf". My compiler says that "scanf" is out of date and I should use "scanf_s" instead. I have a work around for this and others on my computer, but this is not the way to deal with a program.

Overall the code looks OK, but I will have some work before I can test it.

If you are going to work with C++ code this is a good time to start learning what is in the standard name space.

Hope that helps,

Andy

Edit:
Last edited on
First, there are code tags that make posted code easier to read and comment.
Please use them. See http://www.cplusplus.com/articles/jEywvCM9/

I'll repost your code in tags :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

using namespace std;

int main()
{
  int i=0, s=0, a[i], n;
  scanf( "%d", &n );
  for ( i=0; i<n; i++ )
  {
    scanf( "%d", &a[i] );
    if ( a[i] % n == 0 )
    {
      n--;
      a[i] = 0;
    }
    s += a[i];
  }
  printf( "%d", s );

  return 0;
}

You seem to write in C. The header file and the functions scanf() and printf() are from C Standard Library. The C++ can use them, but has more convenient tools for same tasks.


You do have a serious error on line 7. You do create an array there. The a. How many elements does that array have?

The i==0, so you effectively write: int a[0];
How many integers are in a bag that has 0 integers? 0
How many integers can you put into bag that can hold 0 integers? 0

That is the nature of arrays. Their size is fixed.
In your loop the loop counter 'i' gets values 0, 1, 2, .. n-1
None of them is a valid index for array a.
Every time you write to a[i], an out of range error occurs. You overwrite somebody else's memory. The result is undefined behaviour.

You could decide what is the largest n that the user is allowed to give. You could then create an array with that many elements. When the user does give n, you must check that it is not larger than you allow. Then you would have enough space in your array.


You do have an another logical issue. The program does not do what it should.
Lets use your example:
* the user says: n=10

* first input: 1
* 10%1==0 and therefore you decrease n to 9
* you add 0 to sum

* second input: 2
* 9%2 != 0. You add 2 to the sum

That is not what it was supposed to be.

The program should first read all n values in.
Then it should look at all n values and remove the undesirables.
The n updates only after all values have been checked.
If the n does update, then recheck all remaining values with the new n.
Repeat until no more undesirables are found.
Now you can compute the sum of remaining values.
Thank you very much for your time writing this reply.
If the n does update, then recheck all remaining values with the new n.
Repeat until no more undesirables are found.
(I did not understand this part very well)
I just started to learn strings. I am very bad,but i want to improve my knowledge.
I know im boring,but if you have time to give me some hints to solve this problem.
Thank you again :)!
Last edited on
Topic archived. No new replies allowed.