I am trying to print out the contents of an Array into Ascending order, i am also using Functions (part of a task i have been assigned) - i am fairly new to C/C++ hence if anything is a mess.
The Sorting algorithm i have used just outputs all the numbers into a random order, any way to resolve or correct this? - Ideally this needs to output into the 2x2 grid as the Array Displays.
Ascending Order Output
include <stdio.h>/*Seperate Function for Input ArrayInt */
int main(){
int arrayHeight, array[100][2], xCoord, yCoord, i;
printf ("***** Bubble Sort ***** \n");
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;
array[i][1] = yCoord;
}
DisplayArray(array, arrayHeight);
BubbleSort(array, arrayHeight);
}
int DisplayArray(int array[100][2],int arrayHeight, int swap) {
/*Displaying Array elements*/
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]);
}
}
int BubbleSort(int array[100][2], int arrayHeight)
{
/*Sorts the Array elements into appropriate chosen sorting order - Ascending*/
int swap, i, j, k;
for(k =0; k< arrayHeight; k++) {
for (i = 0; i <arrayHeight; i++) {
for (j = i+1; j < 3; ++j) {
if (array[i][0] > array[i][-1]) {
array[i][1] = swap;
swap = array[i][0]; /*Array = Array Name*/
}
}
}
}
printf("\n Printing in Asending Order: ");
{
for (i=0; i<arrayHeight; i++)
{
for (j=0; j<arrayHeight; j++)
{
printf("\n [%d][%d] ", array[i][-1], array[i][1]);
}
}
}
}
Related
I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.
I am trying to output my 2D array into an Ascending order.
I have already output it into the order which the user put in. But can not get it to print to Ascending Order.
Could anybody possibly help with this or know a solution? My code is below for any question regarding code.
int main() {
/* 2D array declaration and size of each Array in the Programme*/
int Array[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &Array[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , Array[i][j]);
if(j==2)
{
printf("\n");
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < 2; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return 0;
}
First Use bubble sort algorithm
for(int i=0; i<size; ++i)
{
for(int j=i+1; j<size; ++j)
{
if(a[i]>a[j]))
{
int temp=a[i];
a[i]=array[j];
a[j]=temp;
}
}
}
Then print your array in an ascending order.
Try to apply your own way, you can use some features of arrays like arrays contiguity.
int *ptr=a;
same with
*(ptr+i), *(ptr+j)
To use a simple one dimensional bubble sort algorithm to sort 2D array you have to add another loop taking care of the rows.
In your case:
for(k =0; k< 2; k++) {
for (i = 0; i < 3; i++) {
for (j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
The program:
#include <stdio.h>
int main(void) {
/* 2D array declaration and size of each Array in the Programme*/
int a[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
// SORT:
for( k = 0; k< 2; k++) {
for ( i = 0; i < 3; i++) {
for ( j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
printf("\n\nAscending : ");
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
return 0;
}
Output:
***** Bubble Sort Assessment 2 *****
Enter numeric values for each Array [0][0]:
1
Enter numeric values for each Array [0][1]:
0
Enter numeric values for each Array [0][2]:
2
Enter numeric values for each Array [1][0]:
5
Enter numeric values for each Array [1][1]:
4
Enter numeric values for each Array [1][2]:
3
The 2-D Array contains :
1 0 2
5 4 3
Ascending :
The 2-D Array contains :
0 1 2
3 4 5
Here is my. I think my code is correct but it gets stuck after i give input. But if i remove other code except sorting and printing it in ascending order it works. But if not it doesn't work.
It stuck here
#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float larges(float data[], int n);
int main()
{
printf("Kolmogorov Test\n");
int n;
float dvalue1;
//printf("No. of elements should not be greater than 20.\n");
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
//sorting in ascending order
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("\nNumbers in ascending order is: \t");
for(int i=0; i<n; i++)
{
printf("%0.2f\t",num[i]);
}
dp = dplus(&num[n], n);
dn = dminus(&num[n], n);
if(dp>dn)
{
dvalue1 = dp;
}
else
{
dvalue1 = dn;
}
//float dalphas = 0.05;
float dvalue = 0.565;
if(dvalue1 < dvalue)
{
printf("\n Since D is less tha Dalpha so the data is unformily distributed.");
}
else
{
printf("\nSince D is greater than Dalpha so the data is not uniformily distributed.");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar = larges(&data[n], n);
return lar;
}
float dminus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar;
lar = larges(&data[n], n);
return lar;
}
float larges(float data[], int n)
{
for(int i=1; i<n; i++)
{
if(data[0]<data[i])
data[0] = data[i];
}
float lar = data[0];
// printf("%f",lar);
return lar;
}
in the while loop which you used to initiating functions d plus and d minus you used while loop which is going for a infinite loop as the count value is not changing in the loop.It is going on and on and on.
You should always check the return value from scanf. That is - scanf returns the number of items matched, so you should do:
if (1 != scanf("%d", &n))
{
// Add error handling
}
If you want to repeat until the input is matched, do:
while(1)
{
printf("Enter number of elements to compute for tets: \t");
if (1 == scanf("%d", &n)) break;
printf("\nIllegal input, try again\n");
}
The same applies when reading in the float values.
That said, I don't think this is the reason, your program is stuck. To debug it, just add more printf statements. Example:
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
printf("Got %d\n", n); // DEBUG PRINT
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
printf("Got all floats\n"); // DEBUG PRINT
//sorting in ascending order
for(int i=0; i<n; i++)
{
printf("Got i = %d\n", i); // DEBUG PRINT
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
and so on.
I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9
I'm trying to ask the user to enter the number of columns and rows they want in a matrix, and then enter the values in the matrix... I'm going to let them insert numbers one row at a time.
How can I create such function ?
#include<stdio.h>
main(){
int mat[10][10],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
scanf("%d",&mat[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%d",mat[i][j]);
}
This works for entering the numbers, but it displays them all in one line... The issue here is that I don't know how many columns or rows the user wants, so I cant print out %d %d %d in a matrix form...
Any thoughts?
Thanks :)
How about the following?
First ask the user for the number of rows and columns, store that in say, nrows and ncols (i.e. scanf("%d", &nrows);) and then allocate memory for a 2D array of size nrows x ncols. Thus you can have a matrix of a size specified by the user, and not fixed at some dimension you've hardcoded!
Then store the elements with for(i = 0;i < nrows; ++i) ... and display the elements in the same way except you throw in newlines after every row, i.e.
for(i = 0; i < nrows; ++i)
{
for(j = 0; j < ncols ; ++j)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
You need to dynamically allocate your matrix. For instance:
int* mat;
int dimx,dimy;
scanf("%d", &dimx);
scanf("%d", &dimy);
mat = malloc(dimx * dimy * sizeof(int));
This creates a linear array which can hold the matrix. At this point you can decide whether you want to access it column or row first. I would suggest making a quick macro which calculates the correct offset in the matrix.
need a
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",mat[i][j]);
}
printf("\n");
}
#include<stdio.h>
int main(void)
{
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
This is my answer
#include<stdio.h>
int main()
{int mat[100][100];
int row,column,i,j;
printf("enter how many row and colmn you want:\n \n");
scanf("%d",&row);
scanf("%d",&column);
printf("enter the matrix:");
for(i=0;i<row;i++){
for(j=0;j<column;j++){
scanf("%d",&mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++){
for(j=0;j<column;j++){
printf("%d \t",mat[i][j]);}
printf("\n");}
}
I just choose an approximate value for the row and column. My selected row or column will not cross the value.and then I scan the matrix element then make it in matrix size.
int rows, cols , i, j;
printf("Enter number of rows and cols for the matrix: \n");
scanf("%d %d",&rows, &cols);
int mat[rows][cols];
printf("enter the matrix:");
for(i = 0; i < rows ; i++)
for(j = 0; j < cols; j++)
scanf("%d", &mat[i][j]);
printf("\nThe Matrix is:\n");
for(i = 0; i < rows ; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d",mat[i][j]);
printf("\t");
}
printf("\n");
}
}
//R stands for ROW and C stands for COLUMN:
//i stands for ROW and j stands for COLUMN:
#include<stdio.h>
int main(){
int M[100][100];
int R,C,i,j;
printf("Please enter how many rows you want:\n");
scanf("%d",& R);
printf("Please enter how column you want:\n");
scanf("%d",& C);
printf("Please enter your matrix:\n");
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
scanf("%d", &M[i][j]);
}
printf("\n");
}
for(i = 0; i < R; i++){
for(j = 0; j < C; j++){
printf("%d\t", M[i][j]);
}
printf("\n");
}
getch();
return 0;
}