I am trying to use Bubblesort for a 2-D array, using a custom sized 2D Array with the max limit of [100][2]. I am a beginner at this, so i am not great at formatting code properly so shedding light would be great.
my input
How many items of data do you wish to enter? 4
Please enter in the X coordinate: 4
Please enter in the Y coordinate: 4
Please enter in the X coordinate: 3
Please enter in the Y coordinate: 3
Please enter in the X coordinate: 2
Please enter in the Y coordinate: 2
Please enter in the X coordinate: 1
Please enter in the Y coordinate: 1
So that prints out how many numbers you wish to enter from a custom array input.
output(Meant to compare each array and switch to ascending order).
Printing in Ascending Order:
[4][3]
[3][3]
[3][3]
It prints 3 arrays not 4, and doesn't print out any of the numbers i entered.
so
But could anybody shed some light on this? It is specifically the Bubblesort function.
int main()
{
int arrayHeight, array[100][2];
printf ("***** Bubble Sort ***** \n");
InputArray(array, arrayHeight);
}
int InputArray(int array[100][2], int arrayHeight, int swap)
{
int i, xCoord, yCoord;
printf("\n How many items of data do you wish to enter? ");
scanf("%d",&arrayHeight);
for(i=0; i<arrayHeight; i++)
{
printf("Please enter in the X coordinate: ");
scanf("%d", &xCoord);
printf("Please enter in the Y coordinate: ");
scanf("%d", &yCoord);
array[i][0] = xCoord;/* Name of XCoordinate and position within Array*/
array[i][1] = yCoord;/*Name of YCoordinate and position within Array*/
}
DisplayArray(array, arrayHeight);
}
int DisplayArray(int array[100][2], int arrayHeight, int swap)
{
int i, j;
printf("\n The 2-D Array contains : \n");
for(i=0; i<arrayHeight; i++)
{
printf("[%d][%d]\n\r", array[i][1], array[i][0]);
}
BubbleSort(array, arrayHeight);
}
int BubbleSort(int array[100][2], int arrayHeight)
{
int swap, i, j, k;
printf("\n Printing in Asending Order: ");
for (i = 0; i <arrayHeight-1; i++)
{
if (array[i][0] > array[i][1 + 1])
{
array[1][i] = array[1][0+1];
swap = array[1][i];
array[i][1 + 1];
printf("\n [%d][%d] ", array[i][0], array[1][i]);
}
}
}
There's a fair amount of things going on here.
Your Question
Your program is printing out strange parts of your array because of the way you're calling printf from within BubbleSort. While BubbleSort is still running, your array isn't fully sorted. However, the function calls printf after each attempt at swapping array elements. If you'd like to print your whole array after sorting, it would be better to allow the sorting function to run to completion, then print out the array in full afterwards.
Other Stuff
There are a lot of points tangential to your question that I'd like to raise to help you out from a style and correctness perspective. Also, some of these are rather interesting.
#include Statements
When compiling this, you should be getting several warnings. If you're using gcc, one of those warnings will be something like:
main.c: warning: incompatible implicit declaration of built-in function ‘printf’
printf ("***** Bubble Sort ***** \n");
This states that the function printf has been implicitly declared when it was called. That is, the compiler inferred that a function printf exists because you called a function named printf. The problem is that it knows nothing about this function other than that it probably exists. This means the complier doesn't know what the function is supposed to return or what arguments it is supposed to accept, so it cannot help you if you use it inappropriately. To avoid this problem, you should include the standard C Input/Output headers in your program like so:
#include <stdio.h>
The stdio.h header file includes many functions besides just printf. Check out man 3 stdio for more information on it.
Define or Declare Functions Before Calling Them
One of the less-modern aspects of C is that it runs through your program from top to bottom. Many modern languages allow you to put your function declarations anywhere in your code and then it works things out on its own. Javascript's variable and function hoisting is an example of this.
Because C does not do this, you should define or declare your functions before you call them. If you call them without a declaration or definiton, the compiler will fall back to the default function signature extern int <function_name>(); That is, if you do not supply a declaration or definition for your function, C will assume that function is defined elsewhere, returns an int, and takes an unspecified number of arguments.
In this program, the function DisplayArray is defined with three arguments:
int DisplayArray(int array[100][2], int arrayHeight, int swap);
However, it is called with only two arguments:
DisplayArray(array, arrayHeight);
This can only happen because when the function is first called, it hasn't yet been defined, so the compiler, without knowing any better, assumes the call is made correctly.
When this is corrected (the definition is put above the first call), the compiler will throw an error stating that the function DisplayArray takes three arguments, but it was called with only two.
Calling Functions / Program Structure
The most oft-cited benefit of creating functions in your code is modularity. This is the idea that you can freely re-use code at different points in your program while knowing what that code is going to do. This program sacrifices this benefit by creating a sort of function-chain, where each function calls the other. InputArray calls DisplayArray, and DisplayArray calls BubbleSort.
This means any time you'd like to print an array, you must be okay with it being bubble-sorted as well. This is considered bad practice because it reduces the amount of times calling the function is useful. A more useful function would be one that displays the array but does not call BubbleSort or modify the array in any way.
Bubble Sorting
Your question doesn't specify exactly how you'd like to bubble sort, but the function here doesn't implement the BubbleSort algorithm. Generally, it's best to make sure you understand the algorithm before applying it to strange cases like 2-D arrays. I've included a working example below, which I hope helps get you on the right track.
Briefly, note that there two loops in a bubble sort:
* an inner loop that runs through the array and swaps neighboring elements
* an outer loop that runs the inner loop until the entire array is sorted
Minor Things
C programs generally prefer snake_case to CamelCase, but more generally, you should do what works best for you. If you're on a team, use a style consistent with that team.
All of the functions in this program return int, yet none of them actually use a return statement or return a useful value. If you have a function does not return a useful value, return void instead (e.g. - void DisplayArray(int array[100][2], int arrayHeight)).
Your displayArray function swaps the position of x and y. printf will display parameters in the order you specify unless directed otherwise. Check out man 3 printf for more on that function.
Working Example
#include <stdio.h>
void DisplayArray(int array[100][2], int arrayHeight)
{
int i, j;
printf("\n The 2-D Array contains : \n");
for(i=0; i<arrayHeight; i++)
{
printf("[%d][%d]\n\r", array[i][0], array[i][1]);
}
}
void BubbleSort(int array[100][2], int arrayHeight)
{
int i;
int swap[2];
int swapHappened = 1;
// the outer loop runs until no swaps need to be made
// no swapping implies the array is fully sorted
while (swapHappened) {
swapHappened = 0;
// the inner loop swaps neighboring elements
// this is what 'bubbles' elements to the ends of the array
for (i = 0; i < arrayHeight-1; i++)
{
if ((array[i][0] > array[i+1][0]) ||
(array[i][0] == array[i+1][0]) && (array[i][1] > array[i+1][1]))
{
// if a swap happens, the array isn't sorted yet, set this variable
// so the `while` loop continues sorting
swapHappened = 1;
// save the higher-value row to a swap variable
swap[0] = array[i][0];
swap[1] = array[i][1];
// push the lower-valued row down one row
array[i][0] = array[i+1][0];
array[i][1] = array[i+1][1];
// put the saved, higher-value row where the lower-valued one was
array[i+1][0] = swap[0];
array[i+1][1] = swap[1];
}
}
DisplayArray(array, arrayHeight);
}
}
int main()
{
int arrayHeight, array[100][2];
printf ("***** Bubble Sort ***** \n");
int i, xCoord, yCoord;
printf("\n How many items of data do you wish to enter? ");
scanf("%d",&arrayHeight);
for(i=0; i<arrayHeight; i++)
{
printf("Please enter in the X coordinate: ");
scanf("%d", &xCoord);
printf("Please enter in the Y coordinate: ");
scanf("%d", &yCoord);
array[i][0] = xCoord;/* Name of XCoordinate and position within Array*/
array[i][1] = yCoord;/*Name of YCoordinate and position within Array*/
}
DisplayArray(array, arrayHeight);
BubbleSort(array, arrayHeight);
DisplayArray(array, arrayHeight);
return 0;
}
I don't get what you are trying to do with your BubbleSort function.
But just to get a few things right :
if (array[i][0] > array[i][1 + 1])
this shouldn't work, your array was initialized as "int array [100][2]". Conventionally the highest number that should be in the second square bracket is 1. (1+1 =2, by the way)
array[1][i] = array[1][0+1];
swap = array[1][i];
C executes code in a sequential basis, so the array[1][i] is overwritten with array[1][0+1] even before the original value is saved in the 'swap' variable.
array[i][1 + 1];
This line of code does not seem do anything.
If you could tell whether you are trying to sort 'by element' or by 'line' (ie by each array in the 2D array), maybe we could help you approach the problem correctly.
Related
I want to get a variable from user and set it for my array size. But in C I cannot use variable for array size. Also I cannot use pointers and * signs for my project because i'm learning C and my teacher said it's forbidden.
Can someone tell me a way to take array size from user?
At last, I want to do this two projects:
1- Take n from user and get int numbers from user then reverse print entries.
2- Take n from user and get float numbers from user and calculate average.
The lone way is using array with variable size.
<3
EDIT (ANSWER THIS):
Let me tell full of story.
First Question of my teacher is:
Get entries (int) from user until user entered '-1', then type entry numbers from last to first. ( Teacher asked me to solve this project with recursive functions and with NO any array )
Second Question is:
Get n entries (float) from user and calculate their average. ( For this I must use arrays and functions or simple codes with NO any pointer )
Modern C has variable size arrays, as follows:
void example(int size)
{
int myArray[size];
//...
}
The size shouldn't be too large because the aray is allocated on the stack. If it is too large, the stack will overflow. Also, this aray only exists in the function (here: funtion example) and you cannot return it to a caller.
I think your task is to come up with a solution that does not use arrays.
For task 2 that is pretty simple. Just accumulate the input and divide by the number of inputs before printing. Like:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float result = 0;
float f;
int n = 0;
printf("How many numbers?\n");
if (scanf("%d", &n) != 1) exit(1);
for (int i=0; i < n; ++i)
{
if (scanf("%f", &f) != 1) exit(1);
result += f;
}
result /= n;
printf("average is %f\n", result);
return 0;
}
The first task is a bit more complicated but can be solved using recursion. Here is an algorithm in pseudo code.
void foo(int n) // where n is the number of inputs remaining
{
if (n == 0) return; // no input remaining so just return
int input = getInput; // get next user input
foo(n - 1); // call recursive
print input; // print the input received above
}
and call it like
foo(5); // To get 5 inputs and print them in reverse order
I'll leave for OP to turn this pseudo code into real code.
You can actually use variable sized arrays. They are allowed when compiling with -std=c99
Otherwise, you can over-allocate the array with an arbitrary size (like an upper bound of your actual size) then use it the actual n provided by the user.
I don't know if this helps you, if not please provide more info and possibly what you have already achieved.
My cousin has a school project and we can't figure out why is the array different the second time it's printed when there is no values changing in between?
Basically you enter a number which states how many rows/columns will the matrix have, and during first loop he assigns a number to every position and prints out the random number. However, the second time we go through the matrix the numbers are different and it seems that they are copied through the matrix from bottom left corner to top right corner for some reason. It seems strange to us because we never assign a different value to a position in the array after defining it for the first time.
int i,j,n,matrica[i][j],suma=0;
srand(time(NULL));
printf("\nunesi prirodan broj N[3,20] = \n");
scanf("%d",&n);
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
matrica[i][j]=rand()%100;
printf("%d, %d = %4d ",i, j, matrica[i][j]);
if(j==n-1) {
printf("\n");
}
}
}
printf("\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d, %d = %4d ", i, j, matrica[i][j]);
if(j==n-1) {
printf("\n");
}
}
}
And here is the result of this (the code I pasted here has 2 prints, and in the image there is 3 but every time you go through the matrix after the first time it's going to be the same):
We need to use malloc to allocate the dynamic amount of memory.
After
scanf("%d",&n) // PS You should check the return value - read the manual page
Put
matrica = malloc(sizeof(int) * n * n);
And declare it as
int *matrica;
Then replace
matrica[i][j]
with
matrica[i * n + j]
And after you have finished with matrica - use free i.e.
free(matrica);
int i,j,n,matrica[i][j]
At this point I must ask, what value do you think i and j will have? Right there you're invoking undefined behaviour by referring to variables declared with automatic storage duration which you've not initialised. Anything after this point is... undefined behaviour.
Having said that, I noticed a few other parts that look strange. Which book are you reading? The reason I ask is that the people I know to be reading reputable textbooks don't have these problems, thus your textbook (or resource, whatever) mustn't be working for you...
I can't read the commentary inside of the string literals, which is a shame, since that's usually quite valuable contextual information to have in a question. Nonetheless, moving on, if this were me, I'd probably declare a pointer to an array n of int, after asking for n, like so:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t n;
printf("Enter n, please: ");
fflush(stdout);
if (scanf("%zu", &n) != 1 || n == 0 || SIZE_MAX / n < n) {
puts("Invalid input or arithmetic overflow...");
return -1;
}
int (*array)[n] = malloc(n * sizeof *array);
if (!array) {
puts("Allocation error...");
return -1;
}
/* now you can use array[0..(n-1)][0..(n-1)] as you might expect */
free(array);
}
This should work for quite high numbers, much higher than int array[n][n]; would in its place... and it gives you that option to tell the user it was an "Allocation error...", rather than just SIGSEGV, SIGILL, SIGBUS or something...
... but nothing would be more optimal than just saving the seed you use to generate the random numbers, and the user input; that's only two integers, no need for dynamic allocation. There's no point storing what rand generates, amd you realise this, right? rand can generate that output purely using register storage, the fastest memory commonly available in our processors. You won't beat it with arrays, not meaningfully, and not... just not.
I am new in c programming and I am studying arrays right now. I saw that unlike java you can't print them automatically by using Arrays.toString() method. I want to write a simple function that prints the array.
In the program it asks us the size of the array and when we write it, it asks what is the value for each element and then the program calls the displayArray() function to print the array on a single line.
For example :
Hello. What will be the size of the new array?
3
Enter the 1. element
7
Enter the 2. element
5
Enter the 3. element
1
The result should be "Your array is: 7 5 1" but instead of that I get "Your array is: 3 6356728 2" as a result. Can you help?
#include <stdio.h>
void displayArray();
int main()
{
int size;
printf("Hello. What will be the size of the new array?\n");
scanf("%d", &size);
int myarray[size];
for (int i = 0; i < size; i++)
{
printf("Enter the %d. element\n" , (i + 1));
scanf("%d", &myarray[i]);
}
displayArray(myarray[size], size);
return 0;
}
void displayArray(int myarray[], int size)
{
printf("Your array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", myarray[i]);
}
return;
}
You have a problem in the function call (which your compiler should have warned you about, read the footnote later)
displayArray(myarray[size], size);
should be
displayArray(myarray, size);
because,
Type mismatch between formal parameter and actual argument: you function expects an array (a pointer to the first element of the array, to be precise), not an element of the array.
undefined behavior. For an array defined as int myarray[size], accessing element like myarray[size] is off-by-one, as C arrays are 0-based indexing.
Footnote:
If you try to compile your code, compiler should complain about the mismatch. It can happen either
You did not turn up the compiler warning level (which is a mistake from your side)
or, you chose to ignore the warnings (which is an "offense" from your side)
It's a common mistake which beginners commit.
While calling a function in which you pass an array, you only need to specify the name of the array you want to pass.
I want to add numbers to an array using scanf
What did i do wrong? it says expected an expression on the first bracket { in front of i inside the scanf...
void addScores(int a[],int *counter){
int i=0;
printf("please enter your score..");
scanf_s("%i", a[*c] = {i});
}//end add scores
I suggest:
void addScores(int *a, int count){
int i;
for(i = 0; i < count; i++) {
printf("please enter your score..");
scanf("%d", a+i);
}
}
Usage:
int main() {
int scores[6];
addScores(scores, 6);
}
a+i is not friendly to newcomer.
I suggest
scanf("%d", &a[i]);
Your code suggests that you expect that your array will be dynamically resized; but that's not what happens in C. You have to create an array of the right size upfront. Assuming that you allocated enough memory in your array for all the scores you might want to collect, the following would work:
#include <stdio.h>
int addScores(int *a, int *count) {
return scanf("%d", &a[(*count)++]);
}
int main(void) {
int scores[100];
int sCount = 0;
int sumScore = 0;
printf("enter scores followed by <return>. To finish, type Q\n");
while(addScores(scores, &sCount)>0 && sCount < 100);
printf("total number of scores entered: %d\n", --sCount);
while(sCount >= 0) sumScore += scores[sCount--];
printf("The total score is %d\n", sumScore);
}
A few things to note:
The function addScores doesn't keep track of the total count: that variable is kept in the main program
A simple mechanism for end-of-input: if a letter is entered, scanf will not find a number and return a value of 0
Simple prompts to tell the user what to do are always an essential part of any program - even a simple five-liner.
There are more compact ways of writing certain expressions in the above - but in my experience, clarity ALWAYS trumps cleverness - and the compiler will typically optimize out any apparent redundancy. Thus - don't be afraid of extra parentheses to make sure you will get what you intended.
If you do need to dynamically increase the size of your array, look at realloc. It can be used in conjunction with malloc to create arrays of variable size. But it won't work if your initial array is declared as in the above code snippet.
Testing for a return value (of addScores, and thus effectively of scanf) >0 rather than !=0 catches the case where someone types ctrl-D ("EOF") to terminate input. Thanks #chux for the suggestion!
I am struggling with the last bit of this program, I need to pass the array to two different functions and I can't figure out how to do it.
The only errors I get occur:
Here:
input(array[20]);
calculate(array[20],&pairs);
and here:
//exit,
exit;
Other than that it should work the way I need it to, I figured out how to use pointers on normal variables but the arrays act differently and I can't figure out what to do...
The documentation is half done and I still have to add the loop at the end that the description outlines, but I just need help with passing the arrays.
Also the error involving my exit line is irrelevant to the question, but if you know a fix that would be great!
/*
Description: Do not use global variables. Pass your arguments by value and
by reference. Using arrays and modular programming techniques,
write a C program that will allow a user to populate an array
with integers, and then compute and print out the number of
adjacent pairs in the array (i.e. the number of occurrences where
an array element is the same as its neighbors). For example, if
the array contained [2,3,3,4,52,52,4,4,4,4,7,7,1], the number of
adjacent pairs is 6. The program should give the user the option
of examining more than one array (i.e. loop). Assume the array has
a max. of 20 elements. The main() function should primarily be
responsible to direct the flow of logic. That is: use one
function to obtain input (pass by reference), another function to
do processing (pass by value), and perhaps a third function to do
output (pass by value). The main() function should call the input
function and the processing function.
*/
//Include statements.
#include <cstdlib>
#include <iostream>
#include <math.h>
//Standard namespace.
using namespace std;
void input (int array[20]); //Used when user inputs the numbers.
void calculate(int array[20], int *pairs); //Used to calculate the matches.
void output(int *pairs); //Used to output the number of pairs.
int main(void)
{
int array[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char quit;
start:
int pairs = 0;
input(array[20]);
calculate(array[20],&pairs);
output(&pairs);
//Ask the user if they want to exit
printf("\nWould you like to continue testing my project, or exit?");
printf("\nTo exit input: 'N' or 'n'. To continue testing input anything else.");
//store their input in variable: exit
scanf("%s",&quit);
//If they want to exit...
if (quit == 'N' || quit == 'n')
{
//exit,
exit;
}
//otherwise,
else
{
//clear the screen
system("cls");
//and go back to the start.
goto start;
}
}
void input(int array[20])
{
int count = 0;
for (count;count<20;count++)
{
printf("Enter values . . . \n");
scanf("%i", &array[count]);
}
}
void calculate(int array[20], int *pairs)
{
int counter = 0;
for (counter;counter<19;counter++)
{
if (array[counter] == array[counter+1])
*pairs+=1;
}
}
void output(int *pairs)
{
printf("Number of pairs: [%i]\n", *pairs);
}
You don't pass in a size when you're calling the function.
input(array);
That should be enough.
Another solution would be to make the function take an int*. You would add an extra parameter to pass in the size of the array.
void func(int* my_array, int size);
You would declare your array like so:
int i[20];
And call your function like this:
func(i, 20);
Currently, by passing in array[20], you're returning an int and you'd be going out of bounds in case you wanted to know since the max index is 19. The inappropriate return type of the expression is not allowing you to compile the program.