14 Replies - 2378 Views - Last Post: 28 February 2012 - 11:30 AM Rate Topic: -----

#1 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Assigning an array to floating point numbers

Posted 21 February 2012 - 06:49 PM

Hello again. Im attempting to write a program that returns an array of 5 floating point numbers. It should return the array according to the user's input, then print out the array in reverse.
I know, to my knowledge, that an array will not print out floating point numbers. So Im taking small steps and I went ahead and attempted to write a program that would return an array of 5 integers according to the users input.

#include <stdio.h>

void function (int  array[]);
int main(void)
{
    int array[5];
    int num;
    int max = 0;
    printf("Enter a number:");
    while (scanf("%d\n", &num))
    {
        printf("%d\n", num);
        num = max++;

    if(max == 5)
    printf("The numbers you entered forward:\n");
    printf("%d", array[num]);
    }
    return 0;
}


Ill go ahead and attach a screenshot of my error.
In this screenshot I entered the numbers 1-5 inclusively. As you can see, some random numbers started appearing after I entered the number 2.
Im at a loss as to handling this anomaly. If anyone has any input, I would greatly appreciate it.
Thank you.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Assigning an array to floating point numbers

#2 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Assigning an array to floating point numbers

Posted 21 February 2012 - 07:20 PM

You are not assigning anything to your array nor you are printing all of your array. I recommend you to take a look at this.
Was This Post Helpful? 1
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Assigning an array to floating point numbers

Posted 21 February 2012 - 07:43 PM

first of all if the array is of type double and has enough elements specified it will hold double (or float) numbers.
double a[5];//will hold 5 doubles starting with a[0] and finishing with a[4].
to load (and print)these array elements you need a loop.e.g.
double num=0.0;
for(int i=0;i<5;i++)
{
  scanf("%f",&num);//get double from keyboard
  a[i]= num;//assign num to a[0]
  num=0;//get num ready for next num entry
}
//use a second loop to print out array 'a' elements in reverse. e.g
for(int i=4;i>=0;i--){//your print statement }

Was This Post Helpful? 1
  • +
  • -

#4 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 07:05 AM

Making progress, I completely overhauled my code according to the tutorial shurd posted. However, when I try to add a second for loop to print the entered numbers backwards, I get an error " redefinition of 'i' ". I tried nesting the last for loop in my program as part of my trial and error process, only to get the error " 'for' loop initial declarations are only allowed in C99 mode ".
Do I need some kind of pointer to 'i' or a second integer?

View Postblu, on 23 February 2012 - 07:01 AM, said:

Making progress, I completely overhauled my code according to the tutorial shurd posted. However, when I try to add a second for loop to print the entered numbers backwards, I get an error " redefinition of 'i' ". I tried nesting the last for loop in my program as part of my trial and error process, only to get the error " 'for' loop initial declarations are only allowed in C99 mode ".
Do I need some kind of pointer to 'i' or a second integer?


Ill add my for loops for printing the numbers.
	for (i = 0; i < 5; ++i)
	{
		printf("Enter a number: ");
		scanf("%lf", &list[i]);
	}

    for (i = 0; i < 5; ++i)

        printf("Numbers = %f\n", list[i]);

    for(int i = 5;i >= 0; i--)

        printf("Numbers in reverse = %f\n", list[i]);


    }




	return 0;
}



View Postblu, on 23 February 2012 - 07:01 AM, said:

Making progress, I completely overhauled my code according to the tutorial shurd posted. However, when I try to add a second for loop to print the entered numbers backwards, I get an error " redefinition of 'i' ". I tried nesting the last for loop in my program as part of my trial and error process, only to get the error " 'for' loop initial declarations are only allowed in C99 mode ".
Do I need some kind of pointer to 'i' or a second integer?


Ill add my for loops for printing the numbers.
	for (i = 0; i < 5; ++i)
	{
		printf("Enter a number: ");
		scanf("%lf", &list[i]);
	}

    for (i = 0; i < 5; ++i)

        printf("Numbers = %f\n", list[i]);

    for(int i = 5;i >= 0; i--)

        printf("Numbers in reverse = %f\n", list[i]);


    }




	return 0;
}


[/quote]

If I new how to edit, I would fix my abysmal post I just made. Im sorry
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 07:47 AM

Since you are using C98 all variables must be defined at the beginning of a scope and you can not define variables inside the body of a for statement. Therefore you need to remove the "int" from inside your for loops.

// for(int i = 5;i >= 0; i--) // Remove the int.
for(i = 5; i >= 0; i--)


The other option would be to compile your code using the current C standard, C11 which does allow creating variables throughout your program. It also allows the variable creation inside the for loop statement.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 10:15 AM

View Postjimblumberg, on 23 February 2012 - 07:47 AM, said:

Since you are using C98 all variables must be defined at the beginning of a scope and you can not define variables inside the body of a for statement. Therefore you need to remove the "int" from inside your for loops.

// for(int i = 5;i >= 0; i--) // Remove the int.
for(i = 5; i >= 0; i--)


The other option would be to compile your code using the current C standard, C11 which does allow creating variables throughout your program. It also allows the variable creation inside the for loop statement.

Jim


Thanks Jim, corrected that little mistake. Talking to my professor this morning I learned that I need to have two arrays, with the array that's intended to have the entered numbers backwards pointing to the original array. Unfortunately for the life of me I can't figure out how to assign my first array to my second.
	double (*list)[5];
	double list2[5];
        int i;
        int x;
	int list = list2;


    for (i = 0; i < 5; ++i)
	{
		printf("Enter a number: ");
		scanf("%lf", &list[i]);
    }

        for (i = 0; i < 5; ++i)
    {
        printf("Numbers forward = %f\n", list[i]);
    }

        for(x = 5;x >= 0; x--)
    {
        printf("Numbers in reverse = %f\n", list2[x]);
    }


Thats what I have for the moment. Im pretty sure my syntax is incomplete in my new code. As usual, Im drawing a blank as to where to continue.
Was This Post Helpful? 0
  • +
  • -

#7 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 10:49 AM

Success. It turns out that I didnt need a second array. I just needed a pointer to the first array that "acted" as my second array.
Thanks to all of you guys that helped out.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 11:25 AM

Whoa, whoa, whoa. This

double (*list)[5];
double list2[5];
       int i;
       int x;
int list = list2;


   for (i = 0; i < 5; ++i)
{
	printf("Enter a number: ");
	scanf("%lf", &list[i]);
   }

       for (i = 0; i < 5; ++i)
   {
       printf("Numbers forward = %f\n", list[i]);
   }

       for(x = 5;x >= 0; x--)
   {
       printf("Numbers in reverse = %f\n", list2[x]);
   }



is wrong! If you have this

double list2[5];


you have an array of 5 elements. Array indexes start from 0, so the valid indexes for this array are 0, 1, 2, 3 and 4. As soon as you do this:

for(x = 5;x >= 0; x--)


You are invoking undefined behavior because 5 is not a valid array index.
Was This Post Helpful? 1
  • +
  • -

#9 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 07:57 AM

Well, thinking I was successful for a couple of days, then I remembered I needed the array to have floating point numbers. I tried the first potential solutions that came to my mind, switching all of the integers to floats. That didn't work. I referred to Jacks array tutorial to no success. Being so close to finishing this up, any tips would be greatly appreciated.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 08:11 AM

Post your current code, plus any error messages this code produces when compiling, exactly as it appears in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 08:17 AM

You should probably post the new code.
Was This Post Helpful? 0
  • +
  • -

#12 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 08:18 AM

#include <stdio.h>

int main(void)
{
    int list [5];
    int i = 0;
    int *ptr = 0;
    ptr = &list[0];
    for (i = 0; i < 5; ++i)
	{
		printf("Enter a number: ");
		scanf("%d", &list[i]);
    }

    for (i = 0; i < 5; ++i)
    {
        printf("Numbers forward = %d\n", list[i]);
    }

    for(i = 4;i >= 0; i--)
    {
        printf("Numbers in reverse = %d\n", *(ptr + i));
    }

    return 0;

}


Here is my code. My objective is to have my integers as floats. I made them as integers as a step to my process of finishing my program. In my opinion integers are simpler to work with. Unfortunately I can not figure out how to change my integers to floats. I want to be able to enter a decimal numbers to be printed instead of whole numbers.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 08:25 AM

What have you tried? Post the code where you tried changing to floating point. Along with any error messages.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 08:41 AM

Just change the int array to a float array and the int pointer to a float pointer. Replace all the "%d"s (for ints) in your printf()s and scanf()s with "%f"s (for floats).

You don't actually need the pointer. Instead of:
*(ptr + i)

You can simply write:
list[i]

This post has been edited by Hezekiah: 28 February 2012 - 08:42 AM

Was This Post Helpful? 1
  • +
  • -

#15 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Assigning an array to floating point numbers

Posted 28 February 2012 - 11:30 AM

I appreciate all of you. My assignments are complete and I can move on.
Thanks
Blu
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1