3 Replies - 490 Views - Last Post: 18 March 2009 - 07:50 PM Rate Topic: -----

#1 twintippr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 09

Just need some guidance

Posted 18 March 2009 - 07:23 PM

Hey I am new to this site and have some sorta basic/ intermidiate questions

C++

I have two problems that i'm not sure how to approach and could use some guidance to write the code.

1) User enters 1.2345678

Using a loop output must be
1.2
1.23
1.234
1.2345
etc.

Heres what i was attempting to do
num = 1.2345678
for(i=2; i<9 ; i++){
out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(i);

cout << num << endl;
}

or is there some other way to do it without using formatting?


2) I have to write a function that receives a double and a numer of places (N) to round to. Function returns value rounded to Nth decimal
ex. (2.146 , 2) returns 2.15

Now this one I'm just lost on. Maybe somehow sorting out each individual number (.1, .04, .006) but if the user enters 2.1323524543 thats alot of variables.
Any hints on this one??



Thanks alot guys! I really appreciate it.


Well i just figuered out what i did wrong on the first one, wasnt paying attention and used out_stream even tho theres no file. somtimes you just got to read it over again.

If there is a way to do it without using formatting id like to hear.

This post has been edited by twintippr: 18 March 2009 - 07:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Just need some guidance

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: Just need some guidance

Posted 18 March 2009 - 07:37 PM

View Posttwintippr, on 18 Mar, 2009 - 08:20 PM, said:

1) User enters 1.2345678


or is there some other way to do it without using formatting?


I would dump it to a buffer & just run through the array with a double loop.

#include <stdio.h>
#include <stdlib.h>

#define LOOPS 6

int main(void) {
  int loop1, loop2, loop_check=1;
  float userin;
  char buff[5];
  printf("Please enter float: ");
  scanf("%f", &userin);

  sprintf(buff, "%.5f",userin);

  for(loop2=0;loop2<LOOPS;loop2++) {
	for(loop1=0;loop1<loop_check;loop1++) {
	  printf("%c",buff[loop1]);
	}
	loop_check++;
	printf("\n");
  }

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 twintippr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 09

Re: Just need some guidance

Posted 18 March 2009 - 07:47 PM

Thanks I should have said C++ but its no problem, i got it to work. still havent gotten the second question to tho =/
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5412
  • View blog
  • Posts: 27,427
  • Joined: 10-May 07

Re: Just need some guidance

Posted 18 March 2009 - 07:50 PM

The 2nd one should be really easy.

I'd start it off the same, dump it to a buffer, then jump to that char. Convert it to an int with atoi(), & then give it the old if>4 routine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1