# How do you remove decimal places in an array?

Page 1 of 1

## 7 Replies - 3950 Views - Last Post: 02 March 2011 - 10:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219775&amp;s=11a7365e105f82dbe29c5978852fd95f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Cnub42

Reputation: 0
• Posts: 4
• Joined: 25-February 11

# How do you remove decimal places in an array?

Posted 02 March 2011 - 02:57 AM

I know to remove decimal places in a double by doing something along the lines of removing all decimals after the hundredth:
```
double d,
int i

d = (int)(d / 10);
d /=10;

```

But how do you do this with the values in an array (with user inputs)? I am working in C. Thank you so much!
Is This A Good Question/Topic? 0

## Replies To: How do you remove decimal places in an array?

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 03:05 AM

Go through each element in the array and work on each element one at a time.

### #3 Cnub42

Reputation: 0
• Posts: 4
• Joined: 25-February 11

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 03:16 AM

janotte, on 02 March 2011 - 03:05 AM, said:

Go through each element in the array and work on each element one at a time.

I'm not so sure how to do that. I haven't been able to find any information on how to modify each element. If I had an array that asked for 10 user-inputs, and I wanted them to be limited to 1 decimal place, how do I change an individual element? I was trying something like:
```array1[x];

x=(int)(x*10);
x/=10;

```

but it doesn't work. I do not know how to address each individual element. Is there some function that will do this so I don't have to modify 10 inputs?

This post has been edited by Cnub42: 02 March 2011 - 03:18 AM

### #4 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 03:18 AM

http://www.cplusplus...utorial/arrays/

### #5 Cnub42

Reputation: 0
• Posts: 4
• Joined: 25-February 11

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 03:51 AM

janotte, on 02 March 2011 - 03:18 AM, said:

http://www.cplusplus...utorial/arrays/

Thank you very much for that link. For some reason, it just clicked with the ways described here. I was able to completely remove the 100th decimal by adding in the line
```printf ("You will need to enter 5 values for this array.\n");
for (i = 0; i < 5; ++i)
{	printf ("\nEnter a value: ");
scanf ("%lf", &input[i]);

input[i]= (int)(input[i] * 10); //these 2 lines remove the 2nd decimal place, without
input[i]/=10;                   // rounding, of number entered by user.
}

```

Is there a more effective way to do this that I missed (again?)? Thank you so much, again.

This post has been edited by Cnub42: 02 March 2011 - 03:51 AM

### #6 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 725
• Joined: 22-December 08

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 04:09 AM

You could use printf() to format your numbers if you want.

This post has been edited by chinchang: 02 March 2011 - 04:09 AM

### #7 Cnub42

Reputation: 0
• Posts: 4
• Joined: 25-February 11

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 06:19 PM

chinchang, on 02 March 2011 - 04:09 AM, said:

You could use printf() to format your numbers if you want.

The only problem I can see with changing the doubles to "%.1lf" with printf is that the values input are used to get values later on in the code. Only the first decimal should be used in the functions to find the average. Unless the numbers are stored as numbers with a single decimal, the values will be skewed later on. I am not sure if I explained that enough now, but I know that I left that part out in my previous question. With that said, is there a way I should do what I did in the last code I posted, or is that sufficient and easy to comprehend if someone were to look at my code later on?

### #8 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: How do you remove decimal places in an array?

Posted 02 March 2011 - 10:34 PM

I suspected output formatting would not be enough for your code given the hints in your question.

The code, along with the comment, looks fine to me as far as readability and comprehension.