# Thread: Questions on HW on array ascending question

1. ## Questions on HW on array ascending question

So, this HW has two parts. First part is to input data and second part is to output data.
Here are the directions of HW.

Part 1 (data input):
Write a C program that prompts the user to enter some data regarding some clients to abusiness. The user should prompt for customer account number (a positive integerbetween 1 and 1000), a last name (a string), and their account balance (a positivefloating point number) as shown below.
Notice how the user input terminates once theuser enters a -999 as the account number:

Enter account number, last name, and balance.
Enter -999 to end input:
? 100 Smith 24.98
? 8000 Jones 334.33
*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***
? 800 Jones 334.33
? 400 Johnson 56.55
? 300 Roberts -330.90
*** Invalid balance amount. Please enter a positive value. ***
? 300 Roberts 330.90
? 500 White 0.00
? 999(What the user types in is shown in blue for clarity only.)
You could store this information in either 3 separate arrays or in an array of type struct(something).
Make the array sizes large enough to hold data for up to 5 clients.

Part 2 (data output):
Once the user has completed entering the data, the program should then SORT THEDATA IN ASCENDING ORDER BY ACCOUNT NUMBER, and then output the clientdata in table form as follows:
ACCOUNT LAST NAME BALANCE
100 Smith 24.98
300 Roberts 330.90
400 Johnson 56.55
500 White 0.00
800 Jones 334.33
If the user enters -999 as the first account number, the program should simply exit,without displaying any information such as:
Enter account number, last name, and balance.
Enter -999 to end input:? 999
Hints:
 If you choose to store the information in arrays, the array for the last name couldlook like: char last_name; and you would prompt as follows: scanf ("%s",last_name[x]); where x is for each client and goes from 0 to 4. When outputting thisinformation, you simply use the %s format specifier again as: printf ("%s",last_name[x]);
 When prompting for the information, use 3 separate scanf statements for eachclient. That is, although the user will enter the information all on 1 line, your scanfstatements can look like the following (assume you have chosen to store your datain arrays: scanf ("%i", &client_num[x]);
scanf ("%s", last_name[x]);
scanf ("%f", &balance[x]);
That way, after you read the client_num, you can test for the value of -999 beforegoing on to read the last_name and balance.

And I tried to make a program according to the instruction, but I am stuck on two parts.
First part is, after showing error message for 'account_balance', there is no ? mark appears as the error message for 'account_number'.
Second part, main one, is I don't know how to fix the ascending code. Please read and make some corrections for me.
Thank you,

Code:
```#include <stdio.h>

int main (void)
{

int i, temp;
int account_number;
char last_name, c;
float account_balance;

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

for (i = 0; i < 10; i++)
{
printf ("? ");
scanf ("%i", &account_number[i]);

if (account_number[i] == -999)
{
printf ("\n");

break;
}

if (account_number[i] < 1 || account_number[i] > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
scanf ("%i", &account_number[i]);
i--;
}

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
scanf ("%f", &account_balance[i]);
i--;
}

} /* End for loop */

for (i = 0; i < 10; i++)
{
if (account_number[i] > account_number[i+1])
{
temp = account_number[i];
account_number[i] = account_number[i+1];
account_number[i+1] = temp;
}
}

printf("ACCOUNT \t\t LAST NAME \t\t\t BALANCE\n");

for (i = 0; i < 10; i++)

{
printf("%i \t\t\t %-30s %.2f\n", account_number[i], last_name[i], account_balance[i]);

}

printf("\n\n");

getchar();

return 0;

} /* End main */``` 2. here is an array of a different type, it needs more logic put to it in how to request information then gather it before printing it all out. as it stands it takes in then prints out to show it's working. more than not.

more to do, yep just a little bit, sorting it too needs to be done, and I trust that you can do it!!!!! Code:
```#include <stdio.h>
#include <stdlib.h> // exit

struct People
{
int account_number;
char *last_name;
float account_balance;
};

struct People account;

int main (void)
{

//  int i, temp;
int max_account = 0;
int account_num = 0;
char *Lname;
float bal = 0.0;

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

while (max_account < 10 || account_num != -999)
{
printf ("? ");
scanf ("%d", &account_num);

// -999 just kill it
if(account_num == -999) {
exit(0);
}
// get the rest
scanf("%s", Lname);
scanf("%f", &bal);
printf("here: %d %s %f\n", account_num, Lname, bal);

while (account_num < 1 || account_num > 1000)
{
printf("reenter your account number, or -999 to quit.\n");
scanf("%d", &account_num);

if (account_num == -999)
continue;
}
account[max_account].account_number = account_num;

printf("%s lname\n\n", Lname);

account[max_account].account_balance = bal;
account[max_account].last_name = Lname;
max_account += 1;

for ( int a = 0; a < max_account; a++)
{
printf("      ACCOUNT LAST NAME BALANCE\n"
"         %d     %s       %.02f\n",
account[a].account_number,
account[a].last_name,
account[a].account_balance);
}

/* commented out

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
scanf ("%f", &account_balance[i]);
i--;
}
* */

} // not here this is end for loop

/* comment out
for (i = 0; i < 10; i++)
{
if (account_number[i] > account_number[i+1])
{
temp = account_number[i];
account_number[i] = account_number[i+1];
account_number[i+1] = temp;
}
}

printf("ACCOUNT \t\t LAST NAME \t\t\t BALANCE\n");

for (i = 0; i < 10; i++)

{
printf("%i \t\t\t %-30s %.2f\n", account_number[i], last_name[i], account_balance[i]);

}

printf("\n\n");

getchar();
* ***/

return 0;

} /* End main */```
it is just a start of an idea. you can toss it or use it. that is up to you.

on your original code did it NOT say that if -999 just quit print nothing out, just quit?
that
Code:
```if ( account_number == -999)
exit(0);```
will kill it while printing nothing out.

as far as sorting it, that is why I put it into a struc to keep everything together, then sort it off the account number then print it out. everything is kept in the same "box" less to keep track of, because it is all put together already.

if you are not doing structs then I'd suggest a 2d array for everything. then
Code:
```// tricky because of float
int account;
char Lname
float bal;```
that first  keeps them organized each one will need to have the same element number to keep them together. so when you sort that account number you still maintain an ability to (try to) keep the names and balance attached to the account number.
Code:
```account [ 1 ] [ acc num in here ] = first account
Lname[ 1 ] [  last name in here ] = first account
bal [ 1.0 ] [ balance in here ]  = first account

account [ 2 ] [ acc num in here ] = second account
Lname[ 2 ] [  last name in here ] = second account
bal [ 2.0 ] [ balance in here ]  = second account```
that is untested. It just looks logical and therefore doable.

going back to read it again. 3. Thank you.
I have worked on the code more by using arrays, but I am stuck on changing the whole last name in orders.
Code:
```#include <stdio.h>
#include <stdbool.h>

int main (void)
{

int i, j;
int temp = 0;
int account_number;
int no_account = 5;
char last_name, c;
char name_temp = 0;
float account_balance;
float balance_temp = 0;
bool valid;

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");
printf ("\n");

for (i = 0; i < 5; i++)
{
printf ("? ");
scanf ("%i", &account_number[i]);

valid = true;

if (account_number[i] == -999)
{
printf ("\n");

break;
}

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_number[i] < 1 || account_number[i] > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
valid = false;
}

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
valid = false;
}

if(valid == false)
{
i--;
}

} /* End for loop */

for(i = 0; i < no_account - 1; i++)
{
for(j = i + 1; j < no_account; j++)
{
if(account_number[i] > account_number[j])
{
// swap logic for account_number
temp = account_number[i];
account_number[i] = account_number[j];
account_number[j] = temp;

// swap logic for last_name
name_temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = name_temp;

// swap logic for account_balance
balance_temp = account_balance[i];
account_balance[i] = account_balance[j];
account_balance[j] = balance_temp;

}
}
}

printf("\nACCOUNT \t LAST NAME \t\t\t BALANCE\n");

for (i = 0; i < account_number[i]; i++)
{
printf("%i \t\t %-30s %.2f\n", account_number[i], last_name[i], account_balance[i]);
}

getchar();

return 0;

} /* End main */``` 4. Originally Posted by ggumbbuk Thank you.
I have worked on the code more by using arrays, but I am stuck on changing the whole last name in orders.
Code:
```#include <stdio.h>
#include <stdbool.h>

int main (void)
{

int i, j;
int temp = 0;
int account_number;
int no_account = 5;
char last_name, c;
char name_temp = 0;
float account_balance;
float balance_temp = 0;
bool valid;

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");
printf ("\n");

for (i = 0; i < 5; i++)
{
printf ("? ");
scanf ("%i", &account_number[i]);

valid = true;

if (account_number[i] == -999)
{
printf ("\n");

break;
}

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_number[i] < 1 || account_number[i] > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
valid = false;
}

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
valid = false;
}

if(valid == false)
{
i--;
}

} /* End for loop */

for(i = 0; i < no_account - 1; i++)
{
for(j = i + 1; j < no_account; j++)
{
if(account_number[i] > account_number[j])
{
// swap logic for account_number
temp = account_number[i];
account_number[i] = account_number[j];
account_number[j] = temp;

// swap logic for last_name
name_temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = name_temp;

// swap logic for account_balance
balance_temp = account_balance[i];
account_balance[i] = account_balance[j];
account_balance[j] = balance_temp;

}
}
}

printf("\nACCOUNT \t LAST NAME \t\t\t BALANCE\n");

for (i = 0; i < account_number[i]; i++)
{
printf("%i \t\t %-30s %.2f\n", account_number[i], last_name[i], account_balance[i]);
}

getchar();

return 0;

} /* End main */```
I made more amendment since this.
However, I still cannot figure out to differentiate 'break' and 'exit'
Code:
```#include <stdio.h>
#include <stdbool.h>

int main (void)
{

int i, j, k;
int temp = 0;
int account_number;
int no_account;
char last_name, c;
char name_temp;
float account_balance;
float balance_temp = 0;
bool valid;

printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");
printf ("\n");

for (i = 0; i < 10; i++)
{
printf ("? ");
scanf ("%i", &account_number[i]);

valid = true;

if (account_number[i] == -999)
{
break;
}

if (account_number[i] < 1 || account_number[i] > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
valid = false;
}

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
valid = false;
}

if(valid == false)
{
i--;
}

no_account = i + 1;

} /* End for loop */

for(i = 0; i < no_account - 1; i++)
{

for(j = i + 1; j < no_account; j++)
{
if(account_number[i] > account_number[j])
{
// swap logic for account_number
temp = account_number[i];
account_number[i] = account_number[j];
account_number[j] = temp;

// swap logic for last_name
for (k = 0; k < 30; k++)
{
name_temp = last_name[i][k];
last_name[i][k] = last_name[j][k];
last_name[j][k] = name_temp;
}

// swap logic for account_balance
balance_temp = account_balance[i];
account_balance[i] = account_balance[j];
account_balance[j] = balance_temp;

}
}
}

printf("\nACCOUNT \t LAST NAME \t\t\t BALANCE\n");

for (i = 0; i < account_number[i]; i++)
{
printf("%i \t\t %-30s %.2f\n", account_number[i], last_name[i], account_balance[i]);
}

getchar();

return 0;

} /* End main */``` 5. I understand that you probably haven't covered structs, but as a side note, they do make something like this a little bit easier.

I may be missing something, but why is this ten:
Code:
`for (i = 0; i < 10; i++)`
on line 25 and the array sizes ten when the instructions say:
Make the array sizes large enough to hold data for up to 5 clients.
I also noticed something about the user input. It looks like it is made all in one line. This makes quite a bit of difference and suggests that instead of getting data one entry at a time using a scanf with one element, that you perhaps should be parsing an fgets or using multiple specifiers and variables in one scanf.

This is how I would write the record entry part if I could not use structs:
Code:
```printf("%s", "? ");
int accEntry = 0;
char nameEntry = "";
double balEntry = 0.0;
scanf("%i%s%f", &accEntry, nameEntry, &balEntry);

for (int i = 0; i < 5 && accEntry != -999; ++i) {
if (accEntry <= 1 || accEntry >= 1000) {
puts("*** Invalid account number.  Please enter 1 - 1000 or -999 to exit ***");
}
else if (balEntry <= 0) {
puts("*** Invalid balance amount.  Please enter a positive value. ***");
}
else {
account_number[i] = accEntry;
strcpy(last_name[i], nameEntry);
account_balance[i] = balEntry;
}

printf("%s", "? ");
scanf("%i%s%f", &accEntry, nameEntry, &balEntry);
}```
I think using a break inside this is unnecessary. 6. I've realized there is a small problem both with my code and with the instruction code. The problem is that after five entries have been successful, the program still asks for a sixth entry even though it will not use the information for the sixth. In the instruction code, when the user enters -999, that would have been the sixth entry. I guess it could be argued whether my code is mistaken or not then, but it probably would be a better idea to not prompt on the sixth iteration.

I also see that I did not deincrement i when an invalid account number or balance were entered. Here is my fixed code:
Code:
```printf("%s", "? ");
int accEntry = 0;
char nameEntry = "";
double balEntry = 0.0;
scanf("%i%s%f", &accEntry, nameEntry, &balEntry);

for (int i = 0; i < 5 && accEntry != -999; ++i) {
if (accEntry <= 1 || accEntry >= 1000) {
puts("*** Invalid account number.  Please enter 1 - 1000 or -999 to exit ***");
--i;
}
else if (balEntry <= 0) {
puts("*** Invalid balance amount.  Please enter a positive value. ***");
--i;
}
else {
account_number[i] = accEntry;
strcpy(last_name[i], nameEntry);
account_balance[i] = balEntry;
}

if (i < 4) {
printf("%s", "? ");
scanf("%i%s%f", &accEntry, nameEntry, &balEntry);
}
}```
I think now I see that it could be that a break might be preferable to this strange if statement I use. Using a break, it would look like this:
Code:
```int accEntry = 0;
char nameEntry = "";
double balEntry = 0.0;
for (int i = 0; i < 5; ++i) {
printf("%s", "? ");
scanf("%i%s%f", &accEntry, nameEntry, &balEntry);

if (accEntry == -999) {
break;
}

if (accEntry <= 1 || accEntry >= 1000) {
puts("*** Invalid account number.  Please enter 1 - 1000 or -999 to exit ***");
--i;
}
else if (balEntry <= 0) {
puts("*** Invalid balance amount.  Please enter a positive value. ***");
--i;
}
else {
account_number[i] = accEntry;
strcpy(last_name[i], nameEntry);
account_balance[i] = balEntry;
}
}```
I still cannot figure out to differentiate 'break' and 'exit'
break gets out of the current loop. exit stops the program from running. Because you have to print out your results, using exit while gathering records should be undesireable. So far in five months of writing code every day I have not found a use for exit. 7. just to clarify I started with exit, then went to continue then stopped refining it when I got to here.
Code:
``` int i, temp;
int account_number;
char last_name;
float account_balance;
printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");

for (i = 0; i < 10; i++)
{
printf ("? ");
scanf ("%i", &account_number[i]);

if (account_number[i] == -999)
{
printf ("\n");
break;
}

if (account_number[i] < 1 || account_number[i] > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
scanf ("%i", &account_number[i]);
i--;
}

scanf ("%s", last_name[i]);
scanf ("%f", &account_balance[i]);

if (account_balance[i] < 0)
{
printf ("*** Invalid balance amount. Please enter a positive value. ***\n");
scanf ("%f", &account_balance[i]);
i--;
}

} /* End for loop */

for (i = 0; i < 10; i++)
{
if (account_number[i] > account_number[i+1])
{
temp = account_number[i];
account_number[i] = account_number[i+1];
account_number[i+1] = temp;
}
}```
though I do think I remember that to means to just quit and not print if -999 is also part of the equation. which this does not have, that is why I use an exit in the first place. kill program no chance of printing anything out logic I think that was my code, I got two separate ones
this is the other one I know for sure, where I am not sure where I left off, I do remember putting in extra loops
for kicks , but the point is no more exit
Code:
```#include <stdio.h>
#include <stdlib.h> // exit
#include <string.h> // strcpy

typedef int bool;
#define true 1
#define false 0

struct People
{
int account_number;
float account_balance;
char last_name; // for them people that got
// really really long last names
};

bool compare(struct People s1,struct People s2)
{
return s1.account_number<s2.account_number;
}

struct People account;

int main (void)
{
int max_account = 0;
int account_num = 0;
char Lname;
float bal = 0.0;

while ( (max_account != 3 ) && ( account_num != -999) )
{
printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input.\n");
printf ("? ");
//    scanf ("%d", &account_num);

scanf("%d %s %f",&account_num, Lname, &bal);

// -999 just kill it or kick it back up to
// top of loop and it goes out of loop
if(max_account == 0 && account_num == -999)
{
//exit(0);
continue;
}
// check account number
else if ( (account_num < 1 || account_num > 1000 ) && account_num != -999)
{ printf("1 here\n");
//while (account_num < 1 || account_num > 1000)
do
{
printf("reenter your account number, or -999 to quit.\n");

scanf("%d", &account_num);

if (account_num == -999)
//    exit(0);
continue;
} while (account_num < 1 || account_num > 1000);
}
else if ( account_num == -999)
//exit(0);
continue;
else
account[max_account].account_number = account_num;

// get the rest
printf("2 here\n");
//    scanf("%s %f", Lname, &bal);

// copy in name
strcpy(account[max_account].last_name, Lname);
printf("account.name %s\n", account[max_account].last_name);

//    printf("here 3\n");
//    scanf("%f", &bal);

//check balance
if(bal < 0.0 )
{
while (bal < 0.0)
{
"their is no way you're that broke\n"
"for a dollar if you have to.\n");
scanf("%f", &bal);

}
}
// if everything is good for bal just assign it
account[max_account].account_balance = bal;

max_account += 1;
printf("max num %d\n", max_account);
printf("Good: %d %s %.02f\n", account_num, Lname, bal);
}// end control while loop

// sort code goes here/ work in progress

// sort and print code goes here
// protective if statment
// continue kicks it out of while loop and
// if no accounts created then do not even try to sort
// or print
// it just exits.
// could be over kill.. testing testing testing
// is needed still

if ( max_account >= 1 )
{
int j,i;
struct People temp;
for(i=1;i<max_account;i++)
{
for(j=0;j<max_account-i;j++)
{
if(account[j].account_number > account[j+1].account_number)
{
temp = account[j];
account[j] = account[j+1];
account[j+1] = temp;
}
}
}
for ( int a = 0; a < max_account; a++)
{
printf("      ACCOUNT LAST NAME BALANCE\n"
"         %d     %s   %.02f\n",
account[a].account_number,
account[a].last_name,
account[a].account_balance);
}
} // end if

return 0;

} /* End main */```
format for print out .. that is just user specs Popular pages Recent additions account, enter, number, scanf, user 