2 Replies - 571 Views - Last Post: 03 February 2011 - 06:52 AM Rate Topic: -----

#1 Quinn90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-November 10

Bank Accounts problem

Posted 03 February 2011 - 03:27 AM

Reaaly having trouble getting my last few functions to work...can any one give me some help?


#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

struct account {
		char surname[11];
		char firstname[11];
		int accountNumber;
		double balance;
 	} ;


void populateAccounts(struct account accounts[], int numAccounts);


void printAccounts(struct account accounts[], int numAccounts);
               
// Sort by account number ascending / descending
void sortByNumber (struct account accounts[], int numAccounts, int dir);
                
// Sort by account number ascending / descending
void sortByBalance (struct account accounts[], int numAccounts, int dir);
               
// Sort by surname ascending / descending
void sortBySurname (struct account accounts[], int numAccounts, int dir);

// Sort by firstname ascending / descending
void sortByFirstname (struct account accounts[], int numAccounts, int dir);

void main()
{ 
	struct account accounts[10];
 
	
	 populateAccounts(&accounts[0], 10);

	 sortByNumber (accounts, 10, 1);
     printf("Sorted by Account Number Ascending:\n");
	 printAccounts(accounts, 10);

	 sortByNumber (accounts, 10, -1);
     printf("Sorted by Account Number Descending:\n");
	 printAccounts(accounts, 10);

	 sortByBalance (accounts, 10, 1);
     printf("Sorted by Balance Ascending:\n");
	 printAccounts(accounts, 10);

	 sortByBalance (accounts, 10, -1);
     printf("Sorted by Balance Descending:\n");
	 printAccounts(accounts, 10);

	 sortBySurname (accounts, 10, 1);
     printf("Sorted by Surname Ascending:\n");
	 printAccounts(accounts, 10);

	 sortBySurname (accounts, 10, -1);
     printf("Sorted by Surname Descending:\n");
	 printAccounts(accounts, 10);

	 sortByFirstname(accounts, 10, 1);
     printf("Sorted by First Name Ascending:\n");
	 printAccounts(accounts, 10);

	 sortByFirstname (accounts, 10, -1);
	 printf("Sorted by First Name Descending:\n");
	 printAccounts(accounts, 10);
} 

              

void populateAccounts(struct account accounts[], int numAccounts)
{ 
	int i; 

	char surnames[10][11] = {"Murphy","Kelly","O'Sullivan","Walsh", "O'Brien","Byrne","Ryan","O'Connor","O'Neill","O'Reilly"};
	char firstnames[10][11] = {"Jack","Sean","Daniel","Conor","James","Sophie","Ava","Emma","Sarah","Grace"};

    srand( time( NULL ) );
	
	for (i=0;i<numAccounts;i++)
	{
		//need to assign surnames and first names from the lists i created above
		 accounts[i].accountNumber = rand() % 10000;
		 accounts[i].balance = double( rand() % 100000);
	}
} 

//Print out a set of bank accounts
void printAccounts(struct account accounts[], int numAccounts)
{ 
   int i; 

   for ( i = 0; i < numAccounts; i++ ) 
   {
	   printf( "%12s\t%12s\t%10d\t%8.2f \n", accounts[i].surname, accounts[i].firstname,accounts[i].accountNumber, accounts[i].balance);
   } 
	printf("\n\n");
} 




// Sort by account number ascending / descending
void sortByNumber (struct account accounts[], int numAccounts, int dir)                
{
	int i, pass;
	struct account temp;
	                    
	for ( pass = 1; pass < numAccounts; pass++ ) 
	{
		
		for ( i = 0; i < numAccounts - 1; i++ ) 
		{
			if(dir == 1)
			{
				if ( accounts[i].accountNumber > accounts[i + 1].accountNumber ) 
				{
					temp = accounts[i];
					accounts[i] = accounts[i + 1];
					accounts[i + 1] = temp;   
				}
			}
			else if (dir == -1)
			{
						// Put your code here to sort by account number descending
	
			}

		}
	}    

}

void sortByBalance (struct account accounts[], int numAccounts, int dir)
{
	int i, pass;
	struct account temp;
	
	// Need to sort balance by both ascending and descending 
}

void sortBySurname (struct account accounts[], int numAccounts, int dir)
{
		// Same thing with surnames
}

void sortByFirstname (struct account accounts[], int numAccounts, int dir)
{
			// Ditto for first names

}


Is This A Good Question/Topic? 0
  • +

Replies To: Bank Accounts problem

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Bank Accounts problem

Posted 03 February 2011 - 03:36 AM

You havn't put any code for the last 3 functions.

And the sorting algorithm you have used in sortByNumber() will also do for the last 3 functions.

What problem are you facing exactly ?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Bank Accounts problem

Posted 03 February 2011 - 06:52 AM

I can understand you being lost on the string sort ( use strcmp, btw ), but straight up numbers? And the second one is just a cut and paste?

Right, here's a hint:
void swapAccount(struct account *a, struct account *b ) {
	struct account temp = *a;
	*a = *b;
	*b = temp;
}

void sortByNumber (struct account accounts[], int numAccounts, int dir) {
	int i, pass;
	for ( pass = 1; pass < numAccounts; pass++ ) {
		for ( i = 0; i < numAccounts - 1; i++ ) {
			if(dir == 1 && accounts[i].accountNumber > accounts[i + 1].accountNumber ) {
				swapAccount(&accounts[i], &accounts[i+1]);
			} else if(dir != 1 && accounts[i].accountNumber /* what do you think? */ accounts[i + 1].accountNumber ) {
				 swapAccount(&accounts[i], &accounts[i+1]);
			}
		}
	}    
}



Your sort is miserable, by the way. Choose a bubble, which is what your compare implies:
void sortByNumber (struct account accounts[], int numAccounts, int dir) {
	int i, swapped = 1;
	while(swapped) {
		swapped = 0;
		for ( i = 0; i < numAccounts - 1; i++ ) {
			if(dir == 1 && accounts[i].accountNumber > accounts[i + 1].accountNumber ) {
				swapAccount(&accounts[i], &accounts[i+1]);
				swapped = 1;



Or choose a selection, which is what your loops imply:
void sortByNumber (struct account accounts[], int numAccounts, int dir) {
	int i, j;
	for ( i = 0; i < numAccounts-1; i++ ) {
		for ( j = i+1; j < numAccounts; j++ ) {
			if(dir == 1 && accounts[i].accountNumber > accounts[j].accountNumber ) {
				swapAccount(&accounts[i], &accounts[j]);



Trying to do both at the same time kills kittens.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1