Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

array of strings c programming help I have a program written that takes an array

ID: 3568584 • Letter: A

Question

array of strings c programming help

I have a program written that takes an array of 15 grades where you can add or remove grades so that you can then choose to find the average, maximum, minimum, print all grades.

I have all that done perfectly, now my problem is creating an array of strings where I can associate a name to each grade,

This way every time I add, remove, print, sort, (but not in the average) The names travel with their respective numbers.

This is what I have done so far. How can I incorporate this to my program??

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

int funcAdd ();
int funcRemove (void);
int funcPrint ();
int funcSort ();
int funcMaxMin ();
int funcAvg ();

char YesNo;
int x;
int y;
int iGrades[15]={98,86,84,89}; //This is the array!
int swap;
int AddGrade;
int counter=4;
int NumGrade;
int size;
int RemoveGrade;
int SortGrade;
int FindMaxMin;
int min_val, max_val;
int CalcAvg;
int nSum=0;
int MenuResponse = 0;
//declare whole function with a name such as AddNumbers

/******************this is the main menu*******************/
main()
{
int funcAdd ();
int funcRemove ();
int funcPrint ();
int funcSort ();
int funcMaxMin ();
int funcAvg ();

char YesNo;
int x;
int y;
int iGrades[15]={98,86,84,89}; //This is the array!
int swap;
int AddGrade;
int counter=4;
int NumGrade;
int size;
int RemoveGrade;
int SortGrade;
int FindMaxMin;
int min_val, max_val;
int CalcAvg;
int nSum=0;
int MenuResponse = 0;
printf(" CALCULATE YOUR GRADES ");
do
{
   sleep(1);
   printf(" 1 Add grades ");
   printf("2 Remove grades ");
   printf("3 Print grades ");
   printf("4 Sort grades ");
   printf("5 Find max/min grades ");
   printf("6 Calculate average ");
   printf("7 Quit ");
   sleep(1);
   printf(" Enter your selection: ");
   scanf("%d", &MenuResponse);
   fflush(stdin);
   switch(MenuResponse)
   {
       /*if function equals a certain input
           else something it should do
           use this for */
       case 1:
           if (MenuResponse=1);
  
               funcAdd();
           break;
       case 2:
           if (MenuResponse=2);
       funcRemove();
           break;
       case 3:
           if (MenuResponse=3);
       funcPrint();
           break;
       case 4:
           if (MenuResponse=4);
       funcSort();
           break;
       case 5:
           if (MenuResponse=5);
       funcMaxMin();
           break;
       case 6:
           if (MenuResponse=6);
       funcAvg();
           break;
   }  

} while (MenuResponse !=7);
printf(" Thanks for using grade calculator! ");
fflush(stdin);
return 0;
}
/*******************END OF MENU FUNCTION!!!**********************/
//problems with counter resetting
//problems with making function; i Don't know where to start

   int funcAdd ()
   {
                               /****help with sizeof!****/
               AddGrade=15-counter;
              
               printf("You have %d grades already, you can enter up to %d grades", counter, AddGrade);
               sleep(1);
               printf(" How many numbers do you want to add? ");
               scanf("%d", &NumGrade);
               printf(" Enter the grades you would like: ");
               for (x=counter; x<counter+NumGrade; x++)  
               {
                   scanf ("%d", &iGrades[x]);
                   fflush(stdin);
               }  
               printf("Now you have %d grades stored", x);  
               return funcAdd;
   }
   int funcRemove ()
   {
           if (MenuResponse=2);
           printf(" Enter the row you would like to remove: ");
           scanf("%d", &RemoveGrade);
           fflush(stdin);
           printf(" The value in row %d is %d, do you still want to delete? ", RemoveGrade, iGrades[RemoveGrade]);
           scanf(" %c", &YesNo);
           fflush(stdin);
           if (YesNo == 'Y'||YesNo == 'y')
           {
           for (x=RemoveGrade; x<counter; x++)
           {
               //remove grade
               iGrades[x]=iGrades[x+1];
           }
           iGrades[counter]=0;
           printf("Your grades are now: ");
           for (x=0; x<counter; x++)
           printf(" %d", iGrades[x]);  
           }
           return funcRemove;
   }
  
   int funcPrint ()
   {
                   for (x=0; x<counter; x++)
           {
               printf(" %d", iGrades[x]);
           }
           return funcPrint;
   }
  
   int funcSort ()
   {
           for (x = 0; x < (counter - 1); x++)
           {
               for (y = 0; y < counter - x - 1; y++)
               {
                   if (iGrades[y] > iGrades[y+1])
                   {
                       swap       = iGrades[y];
                       iGrades[y]   = iGrades[y+1];
                       iGrades[y+1]= swap;
                   }
               }
           }
           for(x=0; x<counter; x++)
           {
               printf(" %d", iGrades[x]);
           }
           return funcSort;
   }
  
   int funcMaxMin ()
   {
       for (x=0; x<counter; x++)
           {
               min_val = iGrades[0];
               max_val = iGrades[0];
               if (iGrades[x] < min_val)
               min_val = iGrades[x];
               if (iGrades[x] > max_val)
               max_val = iGrades[x];
              
           }
           printf("Minimum grade is: %d ", min_val);
           printf("Maximum grade is: %d ", max_val);
           return funcMaxMin;
   }
  
   int funcAvg ()
   {
                   for (x=0; x<counter; x++)
           {
               nSum = nSum + iGrades[x];
           }
           CalcAvg= nSum / counter; // here I calculate the average
           printf(" The average of all your grades is %d ", CalcAvg);
           return funcAvg;
   }
  

Explanation / Answer

The program you have written does not consider the corner cases. The above program when corrected with the bound checking will look something like this

_______________________________________________________________________

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

int funcAdd ();
int funcRemove (void);
int funcPrint ();
int funcSort ();
int funcMaxMin ();
int funcAvg ();

char YesNo;
int x;
int y;
int iGrades[15]={98,86,84,89}; //This is the array!
int swap;
int AddGrade;
int counter=4;
int NumGrade;
int size;
int RemoveGrade;
int SortGrade;
int FindMaxMin;
int min_val, max_val;
int CalcAvg;
int nSum=0;
int MenuResponse = 0;
//declare whole function with a name such as AddNumbers

/******************this is the main menu*******************/
main()
{
    /*
    YOU NEED NOT DECLARE THESE AGAIN
int funcAdd ();
int funcRemove ();
int funcPrint ();
int funcSort ();
int funcMaxMin ();
int funcAvg ();

char YesNo;
int x;
int y;
int iGrades[15]={98,86,84,89}; //This is the array!
int swap;
int AddGrade;
int counter=4;
int NumGrade;
int size;
int RemoveGrade;
int SortGrade;
int FindMaxMin;
int min_val, max_val;
int CalcAvg;
int nSum=0;
int MenuResponse = 0;*/
printf(" CALCULATE YOUR GRADES ");
do
{
//   sleep(1);
   printf(" 1 Add grades ");
   printf("2 Remove grades ");
   printf("3 Print grades ");
   printf("4 Sort grades ");
   printf("5 Find max/min grades ");
   printf("6 Calculate average ");
   printf("7 Quit ");
//   sleep(1);

   //to check if the input was correct
   int validSelectionMade = false;
   while(!validSelectionMade)
   {
       printf(" Enter your selection: ");
       scanf("%d", &MenuResponse);
       fflush(stdin);
       switch(MenuResponse)
       {
           /*if function equals a certain input
               else something it should do
               use this for */
           case 1:
               // you dont need this if you are already using switch ---- if (MenuResponse=1);
                validSelectionMade = true;
                   funcAdd();
               break;
           case 2:
               // you dont need this if you are already using switch ---- if (MenuResponse=2);
           validSelectionMade = true;
           funcRemove();
               break;
           case 3:
               // you dont need this if you are already using switch ---- if (MenuResponse=3);
           validSelectionMade = true;
           funcPrint();
               break;
           case 4:
               // you dont need this if you are already using switch ---- if (MenuResponse=4);
           validSelectionMade = true;
           funcSort();
               break;
           case 5:
               // you dont need this if you are already using switch ---- if (MenuResponse=5);
           validSelectionMade = true;
           funcMaxMin();
               break;
           case 6:
               // you dont need this if you are already using switch ---- if (MenuResponse=6);
           validSelectionMade = true;
           funcAvg();
               break;
               // for default input
           default:
                printf("Please enter a valid option:");
                validSelectionMade = false;
                break;
       }
   }
} while (MenuResponse !=7);
printf(" Thanks for using grade calculator! ");
fflush(stdin);
return 0;
}
/*******************END OF MENU FUNCTION!!!**********************/
//problems with counter resetting
//problems with making function; i Don't know where to start

   int funcAdd ()
   {
                               /****help with sizeof!****/
               AddGrade=15-counter;

               printf("You have %d grades already, you can enter up to %d grades", counter, AddGrade);
//               sleep(1);
               printf(" How many numbers do you want to add? ");
               scanf("%d", &NumGrade);
               if(NumGrade <= AddGrade)
                {
                   printf(" Enter the grades you would like: ");
                   for (x=counter; x<counter+NumGrade; x++)
                   {
                       scanf ("%d", &iGrades[x]);
                       fflush(stdin);
                   }
                   //updates counter
                   counter = counter + NumGrade;
                   printf("Now you have %d grades stored", counter);
                }
                //For keeping a check on how many grades can the user enter
                else
                {
                    printf("you have less than %d spaces left ( only %d left ). Please enter again",NumGrade,AddGrade);
                    funcAdd();
                }
                return 0;
   }
   int funcRemove ()
   {
           //Not required ---- if (MenuResponse=2);
           printf(" Enter the row you would like to remove: ");
           scanf("%d", &RemoveGrade);
           if(RemoveGrade >= counter || RemoveGrade < 0)
           {
               printf("Incorrect row index.Enter again");
               funcRemove();
           }
           else
           {
               fflush(stdin);
               printf(" The value in row %d is %d, do you still want to delete? ", RemoveGrade, iGrades[RemoveGrade]);
               scanf(" %c", &YesNo);
               fflush(stdin);
               if (YesNo == 'Y'||YesNo == 'y')
               {
               for (x=RemoveGrade; x<counter; x++)
               {
                   //remove grade
                   iGrades[x]=iGrades[x+1];
               }
               iGrades[counter]=0;
               //updaet counter
               counter = counter - 1;
               printf("Your grades are now: ");
               for (x=0; x<counter; x++)
                    printf(" %d", iGrades[x]);
               }
               return 0;
           }
   }

   int funcPrint ()
   {
       if(counter == 0)
       {
           printf("There are no numbers to show");
           return 1;
       }
       else
       {
           printf("Numbers in the list are ");
           for (x=0; x<counter; x++)
           {
               printf(" %d", iGrades[x]);
           }
           return 0;
       }
   }

   int funcSort ()
   {
           for (x = 0; x < (counter - 1); x++)
           {
               for (y = 0; y < counter - x - 1; y++)
               {
                   if (iGrades[y] > iGrades[y+1])
                   {
                       swap       = iGrades[y];
                       iGrades[y]   = iGrades[y+1];
                       iGrades[y+1]= swap;
                   }
               }
           }
           //Its good to give an interactive message to the user . About what has happened.
           printf("After sorting, numbers in the list are ");
           for(x=0; x<counter; x++)
           {
               printf(" %d", iGrades[x]);
           }
           return 0;
   }

   int funcMaxMin ()
   {
       if(counter == 0)
       {
           printf("There are no numbers in the list");
           return 1;
       }
       else
       {
           min_val = iGrades[0];
           max_val = iGrades[0];
           for (x=0; x<counter; x++)
           {
               if (iGrades[x] < min_val)
                    min_val = iGrades[x];
               if (iGrades[x] > max_val)
                    max_val = iGrades[x];
           }
           printf("Minimum grade is: %d ", min_val);
           printf("Maximum grade is: %d ", max_val);
           return 0;
       }
   }

   int funcAvg ()
   {
       if(counter == 0)
       {
           printf("There are no numbers in the list");
           return 1;
       }
       else
       {
           for (x=0; x<counter; x++)
           {
               nSum = nSum + iGrades[x];
           }
           CalcAvg= nSum / counter; // here I calculate the average
           printf(" The average of all your grades is %d ", CalcAvg);
           return 0;
       }
   }


_____________________________________________________________________

Coming back to the question you have regarding strings. The simplest way to do that is to make a structure of a grade and a character array. You can read more about structures at http://www.tutorialspoint.com/cprogramming/c_structures.htm . In basic sense , it just packs variables of different types together as one single variable. You can then create an array of these structure objects and use them exactly the same way as you were using iGrades. Tthe program with the structure used can look something like the below. I have commented at a few places to show what is happening. if you have any doubts feel free to contact.

___________________________________________________________________

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

struct GradeAndName
{
    int grade;
    char name[10];
};

GradeAndName iGrades[15]; //This is the array of grades and strings!

int funcAdd ();
int funcRemove (void);
int funcPrint ();
int funcSort ();
int funcMaxMin ();
int funcAvg ();

char YesNo;
int x;
int y;

GradeAndName swap;
int AddGrade;
int counter=0;
int NumGrade;
int size;
int RemoveGrade;
int SortGrade;
int FindMaxMin;
int min_val, max_val;
int CalcAvg;
int nSum=0;
int MenuResponse = 0;
//declare whole function with a name such as AddNumbers

/******************this is the main menu*******************/
main()
{
printf(" CALCULATE YOUR GRADES ");
do
{
//   sleep(1);
   printf(" 1 Add grades ");
   printf("2 Remove grades ");
   printf("3 Print grades ");
   printf("4 Sort grades ");
   printf("5 Find max/min grades ");
   printf("6 Calculate average ");
   printf("7 Quit ");
//   sleep(1);

   //to check if the input was correct
   int validSelectionMade = false;
   while(!validSelectionMade)
   {
       printf(" Enter your selection: ");
       scanf("%d", &MenuResponse);
       fflush(stdin);
       switch(MenuResponse)
       {
           /*if function equals a certain input
               else something it should do
               use this for */
           case 1:
               // you dont need this if you are already using switch ---- if (MenuResponse=1);
                validSelectionMade = true;
                   funcAdd();
               break;
           case 2:
               // you dont need this if you are already using switch ---- if (MenuResponse=2);
           validSelectionMade = true;
           funcRemove();
               break;
           case 3:
               // you dont need this if you are already using switch ---- if (MenuResponse=3);
           validSelectionMade = true;
           funcPrint();
               break;
           case 4:
               // you dont need this if you are already using switch ---- if (MenuResponse=4);
           validSelectionMade = true;
           funcSort();
               break;
           case 5:
               // you dont need this if you are already using switch ---- if (MenuResponse=5);
           validSelectionMade = true;
           funcMaxMin();
               break;
           case 6:
               // you dont need this if you are already using switch ---- if (MenuResponse=6);
           validSelectionMade = true;
           funcAvg();
               break;
               // for default input
           default:
                printf("Please enter a valid option:");
                validSelectionMade = false;
                break;
       }
   }
} while (MenuResponse !=7);
printf(" Thanks for using grade calculator! ");
fflush(stdin);
return 0;
}
/*******************END OF MENU FUNCTION!!!**********************/
//problems with counter resetting
//problems with making function; i Don't know where to start

   int funcAdd ()
   {
                               /****help with sizeof!****/
               AddGrade=15-counter;

               printf("You have %d grades already, you can enter up to %d grades", counter, AddGrade);
//               sleep(1);
               printf(" How many numbers do you want to add? ");
               scanf("%d", &NumGrade);
               if(NumGrade <= AddGrade)
                {
                   printf(" Enter the grades you would like: ");
                   for (x=counter; x<counter+NumGrade; x++)
                   {
                       printf("Enter the grade : ");
                       scanf ("%d", &iGrades[x].grade);
                       printf("Enter the name string : ");
                       scanf ("%s", iGrades[x].name);
                       fflush(stdin);
                   }
                   //updates counter
                   counter = counter + NumGrade;
                   printf("Now you have %d grades stored", counter);
                }
                //For keeping a check on how many grades can the user enter
                else
                {
                    printf("you have less than %d spaces left ( only %d left ). Please enter again",NumGrade,AddGrade);
                    funcAdd();
                }
                return 0;
   }
   int funcRemove ()
   {
           //Not required ---- if (MenuResponse=2);
           printf(" Enter the row you would like to remove: ");
           scanf("%d", &RemoveGrade);
           if(RemoveGrade >= counter || RemoveGrade < 0)
           {
               printf("Incorrect row index.Enter again");
               funcRemove();
           }
           else
           {
               fflush(stdin);
               printf(" The value in row %d is %d, do you still want to delete? ", RemoveGrade, iGrades[RemoveGrade]);
               scanf(" %c", &YesNo);
               fflush(stdin);
               if (YesNo == 'Y'||YesNo == 'y')
               {
                   for (x=RemoveGrade; x<counter; x++)
                   {
                       //remove grade
                       iGrades[x]=iGrades[x+1];
                   }
                   iGrades[counter].grade=0;
                   iGrades[counter].name[0]=''; //empty string
                   //updaet counter
                   counter = counter - 1;
                   funcPrint();
                   return 0;
               }
           }
   }

   int funcPrint()
   {
       if(counter == 0)
       {
           printf("There are no numbers to show");
           return 1;
       }
       else
       {
           printf("Numbers in the list are GRADE NAME ");
           for (x=0; x<counter; x++)
           {
               printf(" %d %s", iGrades[x].grade,iGrades[x].name);
           }
           return 0;
       }
   }

   int funcSort ()
   {
           for (x = 0; x < (counter - 1); x++)
           {
               for (y = 0; y < counter - x - 1; y++)
               {
                   if (iGrades[y].grade > iGrades[y+1].grade)
                   {
                       swap       = iGrades[y];
                       iGrades[y]   = iGrades[y+1];
                       iGrades[y+1]= swap;
                   }
               }
           }
           //Its good to give an interactive message to the user . About what has happened.
           printf("After sorting, ");
           funcPrint();
           return 0;
   }

   int funcMaxMin ()
   {
       if(counter == 0)
       {
           printf("There are no numbers in the list");
           return 1;
       }
       else
       {
           min_val = iGrades[0].grade;
           max_val = iGrades[0].grade;
           for (x=0; x<counter; x++)
           {
               if (iGrades[x].grade < min_val)
                    min_val = iGrades[x].grade;
               if (iGrades[x].grade > max_val)
                    max_val = iGrades[x].grade;
           }
           printf("Minimum grade is: %d ", min_val);
           printf("Maximum grade is: %d ", max_val);
           return 0;
       }
   }

   int funcAvg ()
   {
       if(counter == 0)
       {
           printf("There are no numbers in the list");
           return 1;
       }
       else
       {
           for (x=0; x<counter; x++)
           {
               nSum = nSum + iGrades[x].grade;
           }
           CalcAvg= nSum / counter; // here I calculate the average
           printf(" The average of all your grades is %d ", CalcAvg);
           return 0;
       }
   }

___________________________________________________________________