|
From: lstar36 <ls...@fl...> - 2000-11-20 12:34:06
|
Your problem is that you need to read a file whic has the data in it. You
can either write the array to a file or use your editor to generate a txt
file with the dat in it. Then read in the data. I have modified your program
to show you the last. You try to do the first.
----- Original Message -----
From: "Saundra Schwarz" <Sch...@ho...>
To: <dev...@li...>
Sent: Sunday, November 19, 2000 9:25 PM
Subject: Re: [Dev-C++] Arrays
> The problem is I have no idea where to start.....
>
> The assignment is a generic one in which the professor just has us playing
> around with arrays to get used to using them....What is being asked of me
is
> to change the initialization statement in this program to have the array
> initialized by the file....but I don't know where to begin....
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
>
> void PrintArray(int[], int);
> int SumArray(int[], int);
> int DeleteElement(int[], int, int);
> int InsertIndex(int[], int, int, int);
>
>
> int main()
> {
> // int a[20] = {14,17,23,26,33,38,39,41,52,56,0,0,0,0,0,0,0,0,0,0};
> int a(24);
> FILE *in;
> int i, sum;
>
>
> NumEls = 10,
> DeleteIndex,
> InsertIndex,
> x;
>
> if (in = fopen ("Myfile.txt", "rt")) == NULL)
> {
> FPRINT(STDERR, "CANNOT OPEN INPUT FILE.\N")
> exit(1);
> }
> i=0;
> DO
> {
> a[i] = FGETC(in);
> }WHILE(a(i++) != EOF || i <20);
>
> printf("Array at start of program:\n\n");
> PrintArray(a, NumEls);
> printf("\n\n");
>
> printf("The sum of these elements is %d.\n", SumArray(a, NumEls));
>
> DeleteIndex = 5;
>
> printf("After deletion of %d:\n", a[DeleteIndex]);
>
> NumEls = DeleteElement(a, NumEls, DeleteIndex);
>
> PrintArray(a, NumEls);
> printf("\n\n");
>
>
> printf("Enter a value to insert: ");
> scanf("%d", &x);
> printf("\n\n");
>
> InsertIndex = 6;
>
> NumEls = InsertElement(a, NumEls, InsertIndex, x);
>
> PrintArray(a, NumEls);
>
> system("PAUSE");
> return 0;
> }
>
>
>
>
> void PrintArray(int b[], int NumEls)
> {
> int i;
>
> i = 9;
> printf("The element at index %d is %d.\n\n", i, b[i]);
>
> printf("Array A has %d elements: ", NumEls);
>
> for (i = 0; i < NumEls; i++)
> printf("%3d", b[i]);
>
> printf("\n");
> }
>
>
>
> int SumArray(int b[], int NumEls)
> {
>
> int sum = 0;
> int count = 0;
>
> for(count = 0; count < NumEls; count++)
> {
> sum += b[count];
>
> }//end for statemnt
>
> return sum;
>
> }
>
>
>
> int DeleteElement(int b[], int NumEls, int DeleteIndex)
> {
> int count;
>
> for(count = DeleteIndex; count < NumEls; count++)
> {
>
> b[count] = b[count + 1];
>
> }//end of for statement
>
> NumEls--;
>
> return NumEls;
> }
>
> int InsertElement(int b[], int NumEls, int InsertIndex, int x)
> {
> int count;
>
> for(count = NumEls + 1; count > InsertIndex; count--)
> {
>
> b[count] = b[count - 1];
>
> }
>
>
> b[InsertIndex] = x;
>
> NumEls++;
>
> return NumEls;
> }
>
>
> ----- Original Message -----
> From: Chad Simmons <ho...@ho...>
> To: <dev...@li...>
> Sent: Sunday, November 19, 2000 3:55 PM
> Subject: Re: [Dev-C++] Arrays
>
>
> > >From: "Saundra Schwarz" <Sch...@ho...>
> > >Reply-To: dev...@li...
> > >To: <dev...@li...>
> > >Subject: [Dev-C++] Arrays
> > >Date: Sun, 19 Nov 2000 18:47:00 -0800
> > >
> > >I have an assignment for school that is asking me to include a
procedure
> > >that reads 10 values from a text file to initialize an array. The
> > >professor hasn't covered this aspect of arrays yet and I'm not finding
> the
> > >info in the text....I'm looking for general how to's....thanks.
> > >
> > >Sam
> >
> > Well, there's two main components to this problem. The first is reading
> > numbers from a file. The second is assigning those numbers into an
array.
> > Which part is giving you trouble? (or is it both) Perhaps if you could
> show
> > how far you've gotten so far we may be able to give you some pointers as
> to
> > where to go next.
> >
> > Chad Simmons
> >
_________________________________________________________________________
> > Get Your Private, Free E-mail from MSN Hotmail at
http://www.hotmail.com.
> >
> > Share information about yourself, create your own public profile at
> > http://profiles.msn.com.
> >
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
> >
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
>
|