# C programming (arrays and strings)



## elf (Jul 19, 2002)

Ok the story is I don't have my C compiler available again until tonight or tomorrow. Can anyone tell me if this would function correctly, or check my code to see if you can find any errors? 


```
#include<stdio.h>
#include<string.h>
#include<ctype.h>

main()
{
   int i, h
   FILE *myfile;
   char filename[20];
   int array[];
  
   printf("Enter file name to load: ");
   scanf("%s", filename);
 
   myfile = fopen("%s", filename, "r");

   for(i=0, fscanf( myfile, "%d", &h) != EOF, i++)
      array[i] = h;
}
```
It should prompt for a filename to load, then load that file and scan the values it finds in there into the array called "array". There will be no error checking yet as this just sort of came off the top of my head. It will be fine tuned later.


----------



## LoneWolf071 (Nov 10, 2004)

#include<stdio.h>


main()
{
int i, h;
FILE *myfile;
char filename[20];
int array[80];

printf("Enter file name to load: ");
scanf("%s", filename);

if ((myfile = fopen(filename, "r")) != NULL)
{
for(i=0; fscanf( myfile, "%d", &h) != EOF; i++)
{
array_ = h;
}
fclose(myfile);
}

else
{
printf("The Program Has Failed To Load\n");
}
return 0;
}

you made some grammatical mistakes, you ned ; in the for statement and the int array[] can't be left with an undeclared size, so you should make it 80 just in case. Also, you don't need those extra c libraries. Only stdio.h is needed._


----------



## elf (Jul 19, 2002)

> Also, you don't need those extra c libraries. Only stdio.h is needed.


Well they are used for other parts of the program that haven't been written yet, and that's not really my main(). This is a small function of the greater program (which hasn't been written yet, I'm writing the functions one at a time and this is my second one)

Also, according to my instructor you can leave them with an undeclared size (I think, i missed most of the lectures on arrays)...Something like a dynamic array, one that would get larger as it needed to.


----------



## LoneWolf071 (Nov 10, 2004)

it will only give you an error. you have to declare it a size or it will give you a compiler error. Also, this so far works, but i prints nothing if you run it, just to let you know, can you post the whole SC so i can see it?


----------



## elf (Jul 19, 2002)

This is what I have all together so far. (that function simply loads the array, the other functions will, sort, search, sum, average, and print it)


```
#include<stdio.h>
#include<ctype.h>
#include<string.h>

void menu();
void loadarray();

main()
{
menu();
}

void menu()
{
   int selection, flag1=0, flag2=0;
   do{
   printf("***** Array Processor *****\n\n");
   if(flag1 = 0)
      printf("1 - Load Array\n");
   else if(flag1 = 1)
      printf("1 - Load Array *\n");
   if(flag2 = 0)
      printf("2 - Sort Array\n");
   else if(flag2 = 1)
      printf("2 - Sort Array *\n");
   printf("3 - Search Array\n");
   printf("4 - Sum\n");
   printf("5 - Average\n");
   printf("6 - Print\n");
   printf("7 - Exit\n\n");
   do{
      printf("Enter selection: ");
      scanf("%d", &selection);
      if(selection > 7 || selection < 1){
         printf("Invalid entry, enter a number between 1 and 7\n");
         fflush(stdin);
         continue;
      }
      else if(flag1 != 1 && selection > 1 && selection < 7){
      printf("Error: No array loaded");
      continue;
      }
      else if(flag 1 == 1 && selection > 1 && selection < 8)
        break;
   }while(selection < 1 || selection > 7 || flag1 != 1);
switch(selection){
      case 1:
         flag1 = 1;
         loadarray();
         break;
      case 2:
         flag2 = 1;
         printf("Sort\n");
         break;
      case 3:
         printf("Search\n");
         break;
      case 4:
         printf("Sum\n");
         break;
      case 5:
         printf("Average\n");
         break;
      case 6:
         printf("print\n");
         break;
      case 7:
         break;
      }
   }while(selection != 7)
}

void loadarray()
{
   int i, h
   FILE *myfile;
   char filename[20];
   int array[];
  
   printf("Enter file name to load: ");
   scanf("%s", filename);
 
   myfile = fopen("%s", filename, "r");

   for(i=0, fscanf( myfile, "%d", &h) != EOF, i++)
      array[i] = h;
   printf("%s numbers loaded from %s", i+1, filename);
}
```


----------



## elf (Jul 19, 2002)

*Update*

Well I have had a while to work on it and here is what I have so far. Also, it is looking like I don't need string.h or ctype.h despite what my teacher said...maybe I'll leave them out when I compile just to see if it matters.


```
#include<stdio.h>
#include<ctype.h>
#include<string.h>

void menu();
int loadarray();
void sortarray(int array, int array_length)
void searcharray(int flag2, int array, int array_length);
void sumarray(int array, int array_length);
void averagearray(int sum, int array, int array_length);


main()
{
menu();
}

void menu()
{
   int selection, flag1=0, flag2=0, array_length, sum;
   do{
   printf("***** Array Processor *****\n\n");
   if(flag1 = 0)
      printf("1 - Load Array\n");
   else if(flag1 = 1)
      printf("1 - Load Array *\n");
   if(flag2 = 0)
      printf("2 - Sort Array\n");
   else if(flag2 = 1)
      printf("2 - Sort Array *\n");
   printf("3 - Search Array\n");
   printf("4 - Sum\n");
   printf("5 - Average\n");
   printf("6 - Print\n");
   printf("7 - Exit\n\n");
   do{
      printf("Enter selection: ");
      scanf("%d", &selection);
      if(selection > 7 || selection < 1){
         printf("Invalid entry, enter a number between 1 and 7\n");
         fflush(stdin);
         continue;
      }
      else if(flag1 != 1 && selection > 1 && selection < 7){
      printf("Error: No array loaded");
      continue;
      }
      else if(flag 1 == 1 && selection > 1 && selection < 8)
        break;
   }while(selection < 1 || selection > 7 || flag1 != 1);
   switch(selection){
      case 1:
         flag1 = 1;
         array_length = loadarray();
         break;
      case 2:
         flag2 = 1;
         sortarray(array, array_length);
         break;
      case 3:
         searcharray(int flag2, array, array_length);
         break;
      case 4:
         sumarray(int array, int array_length);
         break;
      case 5:
         averagearray(int array, int array_length);
         break;
      case 6:
         printf("print\n");
         break;
      case 7:
         break;
      }
   }while(selection != 7)
}

int loadarray()
{
   int i, h, array_length;
   FILE *myfile;
   char filename[20];
   int array[];
  
   printf("Enter file name to load: ");
   scanf("%s", filename);
 
   myfile = fopen("%s", filename, "r");
   
   do
   {
      if ((myfile = fopen(filename, "r")) != NULL)
      {
         for(i=0; fscanf( myfile, "%d", &h) != EOF; i++)
            array[i] = h;
         array_length = i + 1;
         fclose(myfile);
      }

      else
         printf("The array failed to load! Check file name.\n");
      
      printf("%s numbers loaded from %s", i+1, filename);
   }while(myfile == NULL);
   return(array_length);
}

void sortarray(int array, int array_length)
{
/* Function incomplete, just holding place until I write it */
}

void searcharray(int flag2, int array, int array_length)
{
/* Function incomplete, just holding place until I write it */
   if(flag2 == 0)
   {
      printf("Performing linear search of the array...flag = %d", flag2);
   }
   else
   {
      printf("Performing binary search of the array...flag = %d", flag2);
   }  
}

void sumarray(int array, int array_length)
{
   int i, sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   printf("Sum of numbers is %d", sum);
}

void averagearray(int array, int array_length)
{
   int i, average, sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   average = sum / array_length;
   printf("Average of numbers is %d", average);
}

void printarray(int array, int array_length)
{
   int i;
   for(i = 0; i < array_length; i++)
      printf("Array[%d]: %d\n", i, array[i])
}
```


----------



## LoneWolf071 (Nov 10, 2004)

```
#include<stdio.h>
#include<ctype.h>
#include<string.h>

void menu();
int loadarray();
void sortarray(int *array, int array_length);
void searcharray(int flag2, int *array, int array_length);
void sumarray(int *array, int array_length);
void averagearray(int sum, int *array, int array_length);


main()
{
menu();
}

void menu()
{
   int selection, flag1=0, flag2=0, array_length, array[80], sum;
   do{
   printf("***** Array Processor *****\n\n");
   if(flag1 = 0)
      printf("1 - Load Array\n");
   else if(flag1 = 1)
      printf("1 - Load Array *\n");
   if(flag2 = 0)
      printf("2 - Sort Array\n");
   else if(flag2 = 1)
      printf("2 - Sort Array *\n");
   printf("3 - Search Array\n");
   printf("4 - Sum\n");
   printf("5 - Average\n");
   printf("6 - Print\n");
   printf("7 - Exit\n\n");
   do{
      printf("Enter selection: ");
      scanf("%d", &selection);
      if(selection > 7 || selection < 1){
         printf("Invalid entry, enter a number between 1 and 7\n");
         fflush(stdin);
         continue;
      }
      else if(flag1 != 1 && selection > 1 && selection < 7){
      printf("Error: No array loaded");
      continue;
      }
      else if(flag1 == 1 && selection > 1 && selection < 8)
        break;
   }while(selection < 1 || selection > 7 || flag1 != 1);
   switch(selection){
      case 1:
         flag1 = 1;
         array_length = loadarray();
         break;
      case 2:
         flag2 = 1;
         sortarray(int *array, array_length);
         break;
      case 3:
         searcharray(flag2,int *array, array_length);
         break;
      case 4:
         sumarray(int *array,array_length);
         break;
      case 5:
         averagearray(sum,int *array,array_length);
         break;
      case 6:
         printf("print\n");
         break;
      case 7:
         break;
      }
   }while(selection != 7);
}

int loadarray()
{
   int i, h, array_length;
   FILE *myfile;
   char filename[20];
   int array[80];
  
   printf("Enter file name to load: ");
   scanf("%s", filename);
 
   myfile = fopen(filename, "r");
   
   do
   {
      if ((myfile = fopen(filename, "r")) != NULL)
      {
         for(i=0; fscanf( myfile, "%d", &h) != EOF; i++)
            array[i] = h;
         array_length = i + 1;
         fclose(myfile);
      }

      else
         printf("The array failed to load! Check file name.\n");
      
      printf("%s numbers loaded from %s", i+1, filename);
   }while(myfile == NULL);
   return(array_length);
}

void sortarray(int *array, int array_length)
{
/* Function incomplete, just holding place until I write it */
}

void searcharray(int flag2, int *array, int array_length)
{
/* Function incomplete, just holding place until I write it */
   if(flag2 == 0)
   {
      printf("Performing linear search of the array...flag = %d", flag2);
   }
   else
   {
      printf("Performing binary search of the array...flag = %d", flag2);
   }  
}

void sumarray(int *array, int array_length)
{
   int i, sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   printf("Sum of numbers is %d", sum);
}

void averagearray(int *array, int array_length)
{
   int i, average, sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   average = sum / array_length;
   printf("Average of numbers is %d", average);
}

void printarray(int *array, int array_length)
{
   int i;
   for(i = 0; i < array_length; i++)
      printf("Array[%d]: %d\n", i, array[i])
}
```

this is what i changes so far, but i'm just 17, i learned from a book, so i'm not that experienced in this, anyone else, i still get about 7 errors and a bunch of warnings, you might want to think about cleaning up this code, you have SOOOOOOOOOOOOO many declarations that it is very hard to figure out which goes where.


----------



## LoneWolf071 (Nov 10, 2004)

```
#include<stdio.h>
#include<ctype.h>
#include<string.h>

void menu();
int loadarray();
void sortarray(int *array, int array_length);
void searcharray(int flag2, int *array, int array_length);
void sumarray(int *array, int array_length);
void averagearray(int sum, int *array, int array_length);


main()
{
 int selection, flag1=0, flag2=0, array_length, array[80], sum;
   do{
   printf("***** Array Processor *****\n\n");
   if(flag1 == 0)
      printf("1 - Load Array\n");
   else if(flag1 == 1)
      printf("1 - Load Array *\n");
   if(flag2 == 0)
      printf("2 - Sort Array\n");
   else if(flag2 == 1)
      printf("2 - Sort Array *\n");
   printf("3 - Search Array\n");
   printf("4 - Sum\n");
   printf("5 - Average\n");
   printf("6 - Print\n");
   printf("7 - Exit\n\n");
   do{
      printf("Enter selection: ");
      scanf("%d", &selection);
      if(selection > 7 || selection < 1){
         printf("Invalid entry, enter a number between 1 and 7\n");
         fflush(stdin);
         continue;
      }
      else if(flag1 != 1 && selection > 1 && selection < 7)
      {
      printf("Error: No array loaded");
      continue;
      }
      else if(flag1 == 1 && selection > 1 && selection < 8)
        break;
   }while(selection < 1 || selection > 7 || flag1 != 1);
   switch(selection)
	{
      case 1:
         flag1 = 1;
         array_length = loadarray();
         break;
      case 2:
         flag2 = 1;
         sortarray(array, array_length);
         break;
      case 3:
         searcharray(flag2,array, array_length);
         break;
      case 4:
         sumarray(array,array_length);
         break;
      case 5:
         averagearray(sum,array,array_length);
         break;
      case 6:
         printf("print\n");
         break;
      case 7:
         break;
      }
   }while(selection != 7);
	return 0;
}

int loadarray()
{
   int i, h, array_length;
   FILE *myfile;
   char filename[20];
   int array[80];
  
   printf("Enter file name to load: ");
   scanf("%s", filename);
 
      if ((myfile = fopen(filename, "r")) != NULL)
      {
         for(i=0; fscanf( myfile, "%d", &h) != EOF; i++)
            array[i] = h;
         array_length = i + 1;
         fclose(myfile);
      }

      else
         printf("The array failed to load! Check file name.\n");
      
      printf("%s numbers loaded from %s", i+1, filename);
    return(array_length);
}

void sortarray(int *array, int array_length)
{
/* Function incomplete, just holding place until I write it */
}

void searcharray(int flag2, int *array, int array_length)
{
/* Function incomplete, just holding place until I write it */
   if(flag2 == 0)
   {
      printf("Performing linear search of the array...flag = %d", flag2);
   }
   else
   {
      printf("Performing binary search of the array...flag = %d", flag2);
   }  
}

void sumarray(int *array, int array_length)
{
   int i, sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   printf("Sum of numbers is %d", sum);
}

void averagearray(int sum, int *array, int array_length)
{
   int i, average;
   sum = 0;
   for(i = 0; i < array_length; i++)
      sum = sum + array[i];
   average = sum / array_length;
   printf("Average of numbers is %d", average);
}

void printarray(int *array, int array_length)
{
   int i;
   for(i = 0; i < array_length; i++)
      printf("Array[%d]: %d\n", i, array[i]);
}
```
sorry to repost, but this is it, it WILL compile with no errors, a few warnings about non-usage, but it will compile, if you add more, please leyt me know if i can help


----------



## elf (Jul 19, 2002)

Does this line open the file?

```
if ((myfile = fopen(filename, "r")) != NULL)
```
I am going to iron out the errors on my own so I can keep up and learn what needs to be fixed, but thanks for the help. I am getting an error opening the file with "too many parameters". Can you open it with just filename? or will that try to open a file named "filename"?


----------



## LoneWolf071 (Nov 10, 2004)

yes, that does open it and assignes it to myfile and does the IF function, it will do a lot, also to many args means one of your functions has to many or too little declarations.


----------



## elf (Jul 19, 2002)

Well I've got it down to some basic warnings

```
warning: passing arg 1 of `sortarray' makes pointer from integer without a cast
```
and it wont' let it compile. I'll probably just ask the teach when I get into class Monday. 

Now all I have to do is write the last two functions and add in comments!


----------

