Sorting an array of integers using pointers - c

I am trying to write a program that sorts an array (input by user) using pointers.
This is the code I wrote:
#include <stdio.h>
void main()
{
int a[100],i,n,j,t;
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter array:\n");
for (i=0; i<n; i++)
{
scanf("%d",a+i);
}
for (i=0; i<(n-1); i++)
{
for (j=i+1; i<n; i++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("Sorted array (ascending) is:\n");
for (i=0; i<n; i++)
{
printf("%d ",*(a+i));
}
printf("\n");
}
The compiler is not showing any errors.
Input:
5
1 5 4 2 8
Output:
1 8 4 2 5
I'm learning by myself and I got the idea that pointers are similar to arrays. I know how to do the same task with arrays but when I try here to substitute them with pointers it doesn't work.
What am I doing wrong here?

the problem is in your second loop while you are sorting:
for (i=0; i<(n-1); i++)
{
for (j=i+1; i<n; i++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
you need to change the counter names to j :
for (i=0; i<(n-1); i++)
{
for (j=i+1; j<n; j++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
this line has the problem :
for (j=i+1; j<n; j++)

I think it is because your j variable is not increased properly and wrong comparison is used (as suggested by Patrick). I think it should be as follow:
for(j=i+1; j<n; j++)

Related

Matrix with the diagonal set to 0, and the rest of the elements set from 1 to n (in C)

Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}

How can i optimize the code for duplicate elements removal?

I have tried to remove the duplicate elements from an array
using c language, here I'am using 3 for loop to remove the duplicate elements.
How can this optimize the code?
any other simple method to remove the duplicate elements?
Thanks.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}

What changes should I make to this code so it solves the system of linear equations?

#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
scanf("%d",&n);//size of matrix
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%f",&A[i][j]);
}
}
//looking for elements in the diagonal matrix
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%f\n",i,x[i]);//solution
}
return(0);
}
it prints things like "#IO". The algorithm is supposed to have three equations with n variables as inputs. I think the problem is when I divide by zero, but I don't know what can I add in order to avoid that.

C finding maximum number in a row of 2D array and replacing it with elements in the upper triangle

Basically i want to create a 2 dimensional array size NxN, find the maximum value in each row and replace it in the upper triangle or rather replace the elements with the maximum it for that specific row, but above the diagonal.
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 100
int main()
{
int n, i, j, temp,m;
float a[MAXIMUM][MAXIMUM], max;
printf("dimensions: ");
scanf("%d",&n);
printf("input elements\n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
scanf("%f",&a[i][j]);
}
}
max=a[0][0];
for (i=0; i<n; i++){
for (j=0; j<n; j++)
{
if(max<a[i][j]){
max=a[i][j];
}
for(m=0;m<n;m++) //the problem starts here
{
if(max>a[i][m]){
if(i+m>n-1){
a[i][m]=max;
}
}
}
}
}
for(i = 0; i < n; i++) {
printf("\n");
for(j = 0; j < n; j++) {
printf("%f ", a[i][j]);
}
}
return 0;
}
J loop finishes every times, then m loop starts so the value of j is n-1 when it is in m loop.That is a problem.
maybe like this
for (i=0; i<n; i++){
max=a[i][0];
for (j=1; j<n; j++){
if(max<a[i][j]){
max=a[i][j];
}
}
for(m=i;m<n;m++){
a[i][m]=max;
}
}

Sudoku Solver Input

I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.
My primary question is: Why is the method I'm using not placing the user input data into the game board array?
#include <stdio.h>
#include <string.h>
#define CELL 81
int main()
{
// Banner
printf("\t\t\t\tSudoku Solver\n");
printf("\t\t\t***************************\n");
//initialize variables
char Game_Board[9][9];
int i,j;
char Game_Buffer[CELL];
int counter = 0;
printf("Please enter the numbers of the board * denotes a blank space\n");
fgets(Game_Buffer,CELL,stdin);
for(i=0;i<strlen(Game_Buffer);i++)
printf("%c", Game_Buffer[i]);
while(counter < 81)
{
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
printf("%d\n", counter);
printf("\t\t\t\t The Board\n");
for( i=0; i<9; i++)
for( j=0; j<9; j++)
{
if( j % 3 == 0)
printf("|");
printf("%c", Game_Board[i][j]);
if(j==8)
printf("|\n");
}
return 0;
}
You probably should use brackets at first place.
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
}
Add all missing brackets and check if your issue still exists.
The counter++ executes after the loop. I have idented the code to show what I mean..
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
You are updating all cells with the same value.

Resources