Problems with array elements from user input, please help 
Author Message
 Problems with array elements from user input, please help

I am trying to wite a program that will find the conjugate and and
intersection of two arrays. However, since I have only been doing C
for less than a couple of weeks now, I am having trouble with even the
basics.
I want two initialise two arrays whose elements can be assigned from
user input. I have desided to tackle the problem by first just getting
to grips with one array, here is the program that I have wrote just to
get and output an array:

#include <stdio.h>

void inputs_array_one(int *pointer, int array_size);

int index, array_size;
int array_one[1]  = {0};

int main(void)
{
int *pointer = array_one;

   printf("\nPlease input how many elements are in the array - ");
   scanf("%d", &array_size);
      while (array_size <= 0)           /* Make sure input is a
positive integer */
      {  printf("\nInput a positive integer for the array size - ");
         scanf("%d", &array_size);
      }
   inputs_array_one(pointer, array_size);

return 0;

Quote:
}

void inputs_array_one(int *pointer, int array_size)
{
   printf("\n");
   for (index=0; index < array_size; index++)
   {  printf("Please input array element - ");
       scanf("%d", &*pointer);
       *pointer++;
   }
   for (index=0; index < array_size; index++)
      printf("\nArray element %d is equal to %d", index,
array_one[index]);
   }

Is this a bad way of assigning user input to elements of an array?
Is there anything I have missed out?
How would I do a similar thing with two arrays?
Could anyone give me any help with finding the conjugate and
intersection of an array?

Sorry if my coding style is a little hard to read and if this message
is a little too long, but I would really appreciate any help.

Thanks Dan Doyle.



Tue, 10 Apr 2001 03:00:00 GMT  
 Problems with array elements from user input, please help


Quote:
> I am trying to wite a program that will find the conjugate and and
> intersection of two arrays. However, since I have only been doing C
> for less than a couple of weeks now, I am having trouble with even the
> basics.
> I want two initialise two arrays whose elements can be assigned from
> user input. I have desided to tackle the problem by first just getting
> to grips with one array, here is the program that I have wrote just to
> get and output an array:

> #include <stdio.h>

> void inputs_array_one(int *pointer, int array_size);

> int index, array_size;
> int array_one[1]  = {0};

The line above only provides space to store ONE and ONLY ONE integer.

Quote:
> int main(void)
> {
> int *pointer = array_one;

This pointer now points to enough space to store ONE and ONLY ONE integer.

Quote:

>    printf("\nPlease input how many elements are in the array - ");
>    scanf("%d", &array_size);
>       while (array_size <= 0)           /* Make sure input is a
> positive integer */
>       {  printf("\nInput a positive integer for the array size - ");
>          scanf("%d", &array_size);
>       }
>    inputs_array_one(pointer, array_size);

> return 0;
> }

> void inputs_array_one(int *pointer, int array_size)
> {
>    printf("\n");
>    for (index=0; index < array_size; index++)
>    {  printf("Please input array element - ");
>        scanf("%d", &*pointer);

The line above is OK, but under the circumstances the & and * cancel each
other out.  You could use just plain pointer here.
Quote:
>        *pointer++;

You don't need the * here.

- Show quoted text -

Quote:
>    }
>    for (index=0; index < array_size; index++)
>       printf("\nArray element %d is equal to %d", index,
> array_one[index]);
>    }

> Is this a bad way of assigning user input to elements of an array?
> Is there anything I have missed out?
> How would I do a similar thing with two arrays?
> Could anyone give me any help with finding the conjugate and
> intersection of an array?

> Sorry if my coding style is a little hard to read and if this message
> is a little too long, but I would really appreciate any help.

> Thanks Dan Doyle.

<Jack>

The minute your inputs_array_one() function stores a second value in you are
writing into memory which does not belong to your program, a serious bug which
produces undefined behavior.

If you want to have an array whose size is determined at run time, you need to
use the memory allocation functions in stdlib.h.  Get the FAQ for comp.lang.c
and read the section on memory allocation.

Also read the section on how useless scanf() is, especially for live human
operator input, and suggestions on better replacements.

c.l.c. FAQ, text & hypertext:
Text ftp://rtfm.mit.edu/pub/faqs/C-faq/faq
Text ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list
Hypertext http://www.eskimo.com/~scs/C-faq/top.html

</Jack>



Wed, 11 Apr 2001 03:00:00 GMT  
 Problems with array elements from user input, please help
: I am trying to wite a program that will find the conjugate and and
: intersection of two arrays. However, since I have only been doing C
: for less than a couple of weeks now, I am having trouble with even the
: basics.
: I want two initialise two arrays whose elements can be assigned from
: user input. I have desided to tackle the problem by first just getting
: to grips with one array, here is the program that I have wrote just to
: get and output an array:

I used to do a lot of work with user-defined arrays of 250 or so elements;
arrays (actually sets of thermodynamic data) could be added, subtracted,
multiplied, and so on, and the results printed out in various forms.  I
found the only practical input method was to use data files, one entry
per line; that way users could build and check their data using an
ordinary editor, and then feed the files into the program.  The program's
output was in the same form as its input, so that the files could be fed
straight back in.  If you're using much over ten elements, I suggest you
take the same approach.

Will



Wed, 11 Apr 2001 03:00:00 GMT  
 Problems with array elements from user input, please help
Thanks for the replies so far, I have now changed my program to the
following:

#include <stdio.h>

void inputs_array_one(int *array_ptr, int array_size);

int temp, array_size, index = 0;
int *array_ptr = 0;

int main(void)
{
   printf("\nPlease input how many elements are in the array - ");
   scanf("%d", &array_size);
      while (array_size <= 0)           /* Make sure input is a
positive integer */
      {  printf("\nInput a positive integer for the array size - ");
         scanf("%d", &array_size);
      }
   array_ptr = (int *) malloc(sizeof(int)*array_size);
   if (array_ptr == NULL)
   {  printf("Not enough memory\n");
      exit(1);
   }

/* Initialize the array */

   while (index <= array_size)
   {  array_ptr[index] = 0;
      index++;
   }
   inputs_array_one(array_ptr, array_size);

return 0;

Quote:
}

void inputs_array_one(int *array_ptr, int array_size)
{
   printf("\n");
   for (index=0; index < array_size; index++)
   {  printf("Please input array element - ");
       scanf("%d", &temp);
       array_ptr[index] = temp;
       array_ptr++;
   }
   for (index=0; index < array_size; index++)
      printf("\nArray element %d is equal to %d", index,
array_ptr[index]);

/* The End */

free(array_ptr);
   }

However the output is incorrect, it seems as if the elements are being
stored one place in the memory ahead of where they should be stored,
in consecutive 4 byte (on my PC) slots.
Could you tell me what is wrong?
I have implemented the advice I have had so far, thanks.

Dan Doyle.



Wed, 11 Apr 2001 03:00:00 GMT  
 Problems with array elements from user input, please help
correction to my last posting:

Quote:
>However the output is incorrect, it seems as if the elements are being
>stored one place in the memory ahead of where they should be stored,
>in consecutive 4 byte (on my PC) slots.

This must have been a fluke, there does seem to be one or two of my
inputs in the output, but mainly it is just garbage like;

0
6
213512579
234987233
-141423442
235424



Wed, 11 Apr 2001 03:00:00 GMT  
 Problems with array elements from user input, please help
I have now nailed my code down to the following, but could someone
help me with the bits which don't work, I have enclosed them with a
comment:

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

int inputs_array_one(int *pointer, int array_size);
void output_array_one(int *pointer, int array_size);

int index, a_size_one, a_size_two, array_size;
int array_one[100], array_two[100];
int *a_one_ptr = array_one;
int *a_two_ptr = array_two;

int main(void)
{

   printf("\nPlease input how many elements are in array one: ");
   scanf("%d", &a_size_one);
   printf("\nPlease input how many elements are in array two: ");
   scanf("%d", &a_size_two);
/* WHILE LOOP GOES HERE */
/* ------------------------------------------------------------
Allocate memory for the pointer
The following code does not work, when I output
the array all elements seem to have been initialised
to 0.

   pointer = (int *) malloc(sizeof(int)*array_size);
      if (pointer == NULL)
      {  printf("Not enough memory\n");
         exit(1);
      }
--------------------------------------------------------------
while loop which doesn't work, it was placed just after I had got the
input
in the main function, marked by WHILE LOOP GOES HERE.

      while (a_size_one | a_size_two <= 0)
      {  printf("\nInput a positive integer for the size of array one
- ");
         scanf("%d", &a_size_one);
         printf("\nInput a positive integer for the size of array two
- ");
         scanf("%d", &a_size_two);
      }
-------------------------------------------------------------- */

   *a_one_ptr = inputs_array_one(a_one_ptr, a_size_one);
   output_array_one(a_one_ptr, a_size_one);

return 0;

Quote:
}

int inputs_array_one(int *pointer, int array_size)
{
   printf("\n");
   for (index=0; index < array_size; index++)
   {  printf("Please input array element - ");
       scanf("%d", &*pointer);
       pointer++;
   }
   pointer = array_one;
   return *pointer;
/* free memory used by pointer -> free(pointer); <- */

Quote:
}

void output_array_one(int *pointer, int array_size)
{     printf("\nThe array is:");
      for (index=0; index < array_size; index++)
   {  printf("\nArray element %d is equal to %d", index,
array_one[index]);
   }
Quote:
}



Wed, 11 Apr 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. c file (stream) input buffer problem help PLEASE!

2. Serial port input problem -- Please help

3. User Input - multiple values to fill an array

4. making pointers to structure elements using array elements

5. Acces the 11th element of a 10 element array

6. checkout compilers (Was: Acces the 11th element of a 10 element array)

7. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

8. Problem accessing user control element from javascript

9. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

10. Problem: array must have at least one element - what is wrong

11. problems with array allocation and destruction::: HELP PLEASE

12. Arrays with no pointers - problem (please help!)

 

 
Powered by phpBB® Forum Software