Assigning an array to floating point numbers

Page 1 of 1

14 Replies - 11618 Views - Last Post: 28 February 2012 - 11:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=267672&amp;s=cc0f88f665a7864ae2b5b615684e5ac1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 blu

Reputation: 0
• 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)

Is This A Good Question/Topic? 0

Replies To: Assigning an array to floating point numbers

#2 shurd

Reputation: 37
• 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.

#3 buffalobill

Reputation: 21
• 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
```

#4 blu

Reputation: 0
• 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?

blu, 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;
}

```

blu, 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

#5 jimblumberg

Reputation: 5471
• Posts: 17,023
• 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

#6 blu

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

Re: Assigning an array to floating point numbers

Posted 23 February 2012 - 10:15 AM

jimblumberg, 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.

#7 blu

Reputation: 0
• 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.

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.

#9 blu

Reputation: 0
• 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.

#10 jimblumberg

Reputation: 5471
• Posts: 17,023
• 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

#11 shurd

Reputation: 37
• 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.

#12 blu

Reputation: 0
• 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.

#13 jimblumberg

Reputation: 5471
• Posts: 17,023
• 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

#14 Hezekiah

Reputation: 208
• 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

#15 blu

Reputation: 0
• 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }