I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. -
#include <stdio.h>
-
#define MAXWORD 101
-
void swap(int *i, int *j);
-
-
int main(void)
-
{
-
int sort[MAXWORD];
-
int size;
-
int i = 1;
-
int j;
-
int pass = 0;
-
-
printf("Enter how many numbers you want sorted. (Max lentgth is 100)\n\n");
-
-
scanf("%d", &size);
-
-
printf("\nEnter the numbers.\n\n");
-
-
for (i = 0; i < size; ++i) /*Fill the array with the numbers input by user*/
-
scanf("%d", &sort[i]); /*Accept the numbers entered by user*/
-
-
printf("\nUnordered data:");
-
-
for (i = 0; i < size; ++i) /*Print the array of numbers that were entered by user*/
-
printf("%5d", sort[i]);
-
printf("\n");
-
-
for (i = 0; i < size; ++i)
-
{
-
-
for (j = i + 1; j < size; ++j)
-
{
-
if (sort[i] > sort[j])
-
-
swap(&sort[i], &sort[j]);
-
-
++pass;
-
-
printf(" After pass %d:", pass);
-
for (i = 0; i < size; ++i)
-
printf("%5d", sort[i]);
-
printf("\n");
-
}
-
-
}
-
return 0;
-
}
-
Please me know if anyone has any ideas on how to print after each pass through the loop.
9 19780 Laharl 849
Recognized Expert Contributor
If you can write a function, say, printArray(), then you can call the function at the end (or beginning, but the end is probably closer to what you want) and have it print the array. If you'd rather not write a function, you can put in the code directly at the end of the loop. By the end, I mean the last line before the closing } in a loop.
I changed a few things and am now able to count each time through the loop and print that back out the appropriate number of times but the array is the same every time. How do I display the contents of the arrary after each pass?
Ganon11 3,652
Recognized Expert Specialist
Maybe you should move this piece of code from the innermost for... loop to the outer for...loop? - ++pass;
-
-
printf(" After pass %d:", pass);
-
for (i = 0; i < size; ++i)
-
printf("%5d", sort[i]);
-
printf("\n");
Maybe you should move this piece of code from the innermost for... loop to the outer for...loop? - ++pass;
-
-
printf(" After pass %d:", pass);
-
for (i = 0; i < size; ++i)
-
printf("%5d", sort[i]);
-
printf("\n");
Here is what I have now. It will print the "After pass %d:" line the correct number of times but everytime the array is the same. -
#include <stdio.h>
-
#define MAXWORD 101
-
int swap(int *i, int *j);
-
-
int main(void)
-
{
-
int sort[MAXWORD];
-
-
int size;
-
int i;
-
int j;
-
int pass = 0;
-
-
-
printf("Enter how many numbers you want sorted. (Max lentgth is 100)\n\n");
-
-
scanf("%d", &size);
-
-
printf("\nEnter the numbers.\n\n");
-
-
for (i = 0; i < size; ++i) /*Fill the array with the numbers input by user*/
-
scanf("%d", &sort[i]); /*Accept the numbers entered by user*/
-
-
printf("\nUnordered data:");
-
-
for (i = 0; i < size; ++i) /*Print the array of numbers that were entered by user*/
-
printf("%5d", sort[i]);
-
printf("\n");
-
-
-
-
for (i = 0; i < size; ++i)
-
-
{
-
for (j = i + 1; j < size; ++j)
-
{
-
if (sort[i] > sort[j])
-
swap(&sort[i], &sort[j]);
-
-
-
++pass;
-
-
}
-
for (pass = 1; pass < size; ++pass)
-
{
-
printf(" After pass %d:", pass);
-
for (i = 0; i < size; ++i)
-
printf("%5d", sort[i]);
-
printf("\n");
-
-
}
-
-
}
-
-
}
-
Ganon11 3,652
Recognized Expert Specialist
Have you tried printing the array just before the return statement in main()? You may not even be sorting it correctly, which would explain why the array never changes. I suspect it's something to do with the swap function, which you have not shown us yet.
I've moved my printf statements down to between the end brackets (lines 53 - 55) and it prints the final sort. Below is my swap function. Sorry I forgot it - I'm getting pretty stressed over this. - (C) #include <stdio.h>
-
-
int swap(int *i, int *j)
-
{
-
-
-
int tmp;
-
-
tmp = *i;
-
*i = *j;
-
*j = tmp;
-
-
return 0;
-
-
}
Ganon11 3,652
Recognized Expert Specialist
So, just to clear up the situation:
1) Your bubble sort is actually sorting correctly, as indicated by the final printf statement.
2) Your printf statements in the middle of the sort are not printing the array's progress correctly - they merely print the original array.
I'm not sure what's going on. The only thing I can suggest is making a separate print_array function that accepts an int* to the array and an int with its size, and that prints the first size elements of the array, then a newline. This will at least make your code a little easier to read. I'll take a look back over your code and see what I can do.
EDIT: Why is the printing section inside a for...loop? You want it to execute once per execution of the first for...loop (thus printing after every time your inner loop has 'bubbled' a number to the right/left), so you should just have the statements to print the array. Additionally, this for...loop is resetting the value of pass, so that variable is now useless.
Thanks so much for your help. I started completely over and used a print array function and it worked. (Big sigh!)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Gram |
last post by:
Hello,
Can anyone help out with a bubble sort for strings using ASP? I have a text
file of words id like to sort and count.
Thanks in advance for any help.
Gram.
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
|
by: Mark Kamoski |
last post by:
Hi--
Please help.
I need a code sample for bubble sort.
Thank you.
--Mark
|
by: Chris |
last post by:
I have a bubble sort for a 2-dimensional array that sorts a
string,number pair based on the number. The code for the sort is as
follows:
Private Sub SortArray(ByRef roundarray(,) As String)
Dim i, j, x As Integer
x = roundarray.GetUpperBound(0)
Dim tempname, tempnumber As String
For i = 0 To x - 1
For j = i + 1 To x
|
by: Slowjam |
last post by:
How do I correct the program below to search all the anagrams in a given file.
First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for "word", or "eelttr" for "letter".
Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words.
For instance, if I had ,
our initial array of keys...
| |
by: guest |
last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for integers. How do i get a bubble sort to put names in alphabetical order?
|
by: midknight5 |
last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold multiple values.
I need to make a bubble sort that reaches into an array called Tree which has in each storage location a class called Christmas Tree which have the values Species, Height, trunkDiameter, and costPerFoot. The sort will need to sort...
|
by: xtheendx |
last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The program then should sort the names alphabetically by last name and display a list of all the students names and grades in alphabetical order. I have completed all my functions and classes, but i am getting a god aweful amount of errors and i am not...
|
by: mahdiahmadirad |
last post by:
Hi dears!
I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping this array. please help me.
my scenario:
assume that we have a big 2d char array for example students for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name.
no i want to sort this array...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |