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

c++ Read from ExpenseData.txt the dollar amounts and the store names into two ar

ID: 3693389 • Letter: C

Question

c++

Read from ExpenseData.txt the dollar amounts and the store names into two arrays (double and string) then display all the data on the screen.

Create two arrays (storeName as string, and totalSpent as double). Read the store names from the file stores.txt and store these names into the array storeName. Then read from the ExpenseData.txt file the dollar amount and store name and depending on the store name add the dollar amount into the totalSpent array of the specific store. at then end, display how much you spent in each store.

then display the amount spent in each store in decreasing order (high to low).

Explanation / Answer

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


void main()
{
double totalSpent[20],amount;
char storeName[20][30];
char str[30],stnm[30];
FILE * file, *file1;
int i=0,n=0,j=0,swap=0;
file = fopen( "stores.txt" , "r");
file1 = fopen( "ExpenseData.txt" , "r");
for(i=0;i<20;i++)
   totalSpent[i]=0;
if (file) {
   for (i=0;fscanf(file, "%s", str)!=EOF;i++)
   {
   strcpy(storeName[i],str);
   n++;
   }
   fclose(file);
}

if (file1)
{
printf("infile1");
   while(fscanf(file1, "%s %f", str,amount)!=EOF)
   {

   for(i=0;i<n;i++)
   {
   if(strcmp(storeName[i],str)==0)
   break;
   }
   totalSpent[i] += amount;
   }
}
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < n - j - 1; j++)
{
if (totalSpent[j] > totalSpent[j+1]) /* For decreasing order use < */
{
swap = totalSpent[j];
totalSpent[j] = totalSpent[j+1];
totalSpent[j+1] = swap;
   strcpy(str,storeName[j]);
   strcpy(storeName[j],storeName[j+1]);
   strcpy(storeName[j+1],str);
}

}
}
for(i=0;i<n;i++)
{
   printf("%s:-%d",storeName[i],totalSpent[i]);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote