7 Replies - 2565 Views - Last Post: 02 March 2011 - 10:34 PM Rate Topic: -----

#1 Cnub42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Cnub42  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 March 2011 - 03:16 AM

View Postjanotte, 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

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

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

Posted 02 March 2011 - 03:18 AM

Read how arrays work here
http://www.cplusplus...utorial/arrays/
Was This Post Helpful? 1
  • +
  • -

#5 Cnub42  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 March 2011 - 03:51 AM

View Postjanotte, on 02 March 2011 - 03:18 AM, said:

Read how arrays work here
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

Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 Cnub42  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 March 2011 - 06:19 PM

View Postchinchang, 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?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1