Page 1 of 1

Fix this code upload code & output #include #include int main( ) { int *p; typedef struct funds { in

Posted: Mon May 02, 2022 12:08 pm
by answerhappygod
Fix this code upload code & output
#include<stdio.h>
#include <string.h>
int main( )
{
int *p;
typedef struct funds
{
int id;
char bank[10];
float balance;
}fund;
float sum(fund *);
int cust()
{
fund record1 = {1, "BOA ", $1023.43}; /*char values
would need to be converted to integer or float for structure
fields*/
fund record2 = {2, "Chase ", $4239.87};
float s;
s = sum(&record1);
s = sum(&record2);
printf("Customer has a total of $
%0.2f",s);
return 0;
}
float sum(fund *record)
{
static float total;
total = total + record->balance;
return total;
}
int cust[6] = { 0, 1023.43, 4239.87,
0, 0, 0 } ;
p =
&cust[0];
for ( int i = 0 ; i<6 ; i++
)
{
printf("cust[%d]:
balance is %d and address is %p\n", i, *p, p);
p++;
}
return 0;
}
The sum function receives as a
pointer record to funds structure.
Thus, passing the memory address of record from main to function
sum causes the pointer record in sum to
point to
the funds structure. In the
sum function use the ->
operator to gain
the balance values.
The function should have float data type variable
called total to retain a
“running sum of balance” and
variable p which holds the memory
address of total. Assign value of total to pointer p.
Use pointer p to make the value of total known in main.
Initialize record fields to equal using pointer to
array values the values of cust[0] cust[1] cust[2]

Then call sum pass using a unary
operator the memory address of record, the
function keeps cumulative sum of all balance as float data type in
local variable total.

Initialize record fields to equal using
pointer to array values the values of cust[3] cust[4]
cust[5]

Then call sum pass using a unary
operator the memory address of record, the
function keeps cumulative sum of all balance as float data type in
local variable total.