8 Replies - 3577 Views - Last Post: 14 February 2010 - 10:26 AM Rate Topic: -----

#1 9miles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

number - roman convertion(* Pointers help)

Posted 14 February 2010 - 06:33 AM

ive been trying a program with these conditions:

Write a program that accepts an ordinary number and output. Its equivalent roman numerals. The ordinary numbers and their equivalent roman numerals are given below

1 I
5 V
10 X
50 L
100 C
500 D
1000 M
*use pointer to give value to the number
Sample: 2968
MMCM LXVIII

and this is what ive done so far

#include<stdio.h>
#include<conio.h>



main()
{

int n,n1,i,k;

int a[]={1000,500,100,50,10,5,1};

char b[]={'M','D','C','L','X','V','I'};
char * mypointer;

clrscr();

printf("Input a number ");

scanf("%d",&n);

printf("\nROMAN equivalent of %d is ",n);

for (k=0;k<7;k++)
{

n1=n/a[k];

for(i=0;i<n1;i++)
printf("%c",*mypointer[k]);

n=n%a[k];

}
}


now im getting error which says mypointer is declared but never used in fuction main and b is declared but never used in function main,.. i guess because on my pointers value,..

can anyone help put?its been my probem for 2 days lol

Is This A Good Question/Topic? 0
  • +

Replies To: number - roman convertion(* Pointers help)

#2 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 06:43 AM

mypointer is not pointing to anything useful.
you can get rid of mypointer completely when you change this line:

printf("%c",*mypointer[k]);



into:

printf("%c",b[k]);



Good luck

EDIT: or do what n8wxs suggests!

This post has been edited by Ferencn: 14 February 2010 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 06:47 AM

The errors are correct. :)

You declare the pointer and the array:
...
char b[]={'M','D','C','L','X','V','I'};
char * mypointer;
...


but never assign a value to the pointer variable:

mypointer = b;



edit:
Change:

...
for (k=0;k<7;k++) {
    n1=n/a[k];

    for(i=0;i<n1;i++)
        printf("%c",*mypointer[k]);

    n=n%a[k];
}
...



to

...
for (k=0;k<7;k++) {
    n1=n/a[k];

    for(i=0;i<n1;i++)
        printf("%c", mypointer[k]); /* don't need asterisk */

    n=n%a[k];
}
...


This post has been edited by n8wxs: 14 February 2010 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 9miles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 06:49 AM

yeah it would word if i make
printf("%c",*mypointer[k]);
to
printf("%c",b[k]);

but what im trying to do is to *se pointer to give value to the number,...thats one of the instructions,..any help pls?
Was This Post Helpful? 0
  • +
  • -

#5 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 06:51 AM

You are already using a pointer to assign the value to the number! the command:
scanf("%d",&n);


passes a pointer. What &n means is 'get the address of where n is stored in memory' such an address is actually a pointer.

This post has been edited by Ferencn: 14 February 2010 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 9miles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 07:03 AM

can anybody check this,..i want to known if mypointer = b
is being use to give value to the number or is it just useless?

#include<stdio.h>
#include<conio.h>



main()
{

int n,n1,i,k;

int a[]={1000,500,100,50,10,5,1};

char b[]={'M','D','C','L','X','V','I'};
char * mypointer;
mypointer = b;
clrscr();

printf("Input a number ");

scanf("%d",&n);

printf("\nROMAN equivalent of %d is ",n);

for (k=0;k<7;k++)
{

n1=n/a[k];

for(i=0;i<n1;i++)
printf("%c",b[k]);
n=n%a[k];

}
}

Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 07:08 AM

Here you made 'mypointer' to point to 'b'. So you could just modify the printf():
printf("%c",mypointer[k]); // mypointer[k] is equivalent with b[k]


In this case, you are only using the pointer to print values. You do not assign anything to it anymore ...
EDIT: you should use "int main()", not "main()".
EDIT2: even if we post here and help you, you will still not understand what pointers does. Please ask your teacher or consult a book.
Here are some online tutorials too:
http://www.cplusplus...orial/pointers/
http://www.augustcou...torial/ptr.html
http://www.linuxconf...anding-pointers

This post has been edited by sarmanu: 14 February 2010 - 07:12 AM

Was This Post Helpful? 2
  • +
  • -

#8 9miles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 07:53 AM

okay i cant ask my instructor because this is suppose to be a final exam for me.

ive been reading for some tutorial and this is what i made,..it runs but give me a wrong value. i mean if i input 999 is suppose to be MMMMMMM2 so i guess im near to finish this and hope you guys can help me out.

heres the code:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>



main()
{

int n,n1,i,k;

int a[]={1000,500,100,50,10,5,1};
int *p1;

p1 = a;
*p1 = 'M','D','C','L','X','V','I';
clrscr();

printf("Input a number ");

scanf("%d",&n);

printf("\nROMAN equivalent of %d is ",n);

for (k=0;k<7;k++)
{

n1=n/a[k];

for(i=0;i<n1;i++)
printf("%c",a[k]);\\\\\\\\\\\\\\i think something is wrong in this column

n=n%p1[k];

}
}


now my only problem is i want to give a variable for *p1 = 'M','D','C','L','X','V','I'; so i could put the variable to printf("%c",a[k])
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: number - roman convertion(* Pointers help)

Posted 14 February 2010 - 10:26 AM

The point of pointers is generally to pass information around. If you're not passing information somehow, it's probably wrong.

My program would probably look like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOOKUP_SIZE 7
struct RomanItemStruct {
	int num;
	char *roman;
} lookUp[LOOKUP_SIZE] = {{1000,"M"},{500,"D"},{100,"C"},{50,"L"},{10,"X"},{5,"V"},{1,"I"}};

typedef struct RomanItemStruct RomanItem;

// sorry, you have to write these two
RomanItem *lookupMaxValue(int n);
const char *toRoman(int n);

int main() {
	int n;
	
	printf("Input a number ");
	scanf("%d",&n);
	printf("\nROMAN equivalent of %d is %s\n", n, toRoman(n));
	
	return 0;
}



Now you've got pointers to structs and pointers to string, all the way they're meant to be used.

For something more simple, you needn't pass back a string from toRoman, just print it there.

void toRoman(int n);

printf("\nROMAN equivalent of %d is ", n);
toRoman(n);



Good luck.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1