Can anyone tell me the problem with my code? - c

Everythings right except the output of the last part, I'm supposed to get this output https://i.stack.imgur.com/PRJMT.png but instead it got this https://i.stack.imgur.com/iZflq.png
Also heres my code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of goal-scorers: ");
scanf("%d", &n);
int ar[n];
for(int i=0; i<n; i++){
printf("Score of player #%d: ", i+1);
scanf("%d", &ar[i]);
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(ar[i] <ar[j]){
int temp = ar[j];
ar[j] = ar[i];
ar[i]; temp;
}
}
}
printf("\nHighest to lowest:\n");
for(int i=0; i<n; i++)
printf("Player #%d: %d\n",i+1, ar[i]);
return;
}

Found it!
ar[i]; temp;
But you want ar[i] = temp;
You enabled compiler warnings. What I don't get is why the warning for "statement has no effect" is not showing up.

The possible solution may be like :
#include <stdio.h>
struct player {
int no;
int score;
};
int main() {
int n;
printf("Enter the number of goal-scorers: ");
fflush(stdout);
scanf("%d", &n);
if (n <= 0)
return 0 ;
struct player players[n];
for (int i = 0 ; i < n ; i++) {
players[i].no = 1 + i;
printf("Score of player #%d: ", players[i].no);
fflush(stdout);
scanf("%d", &players[i].score);
}
for (int i = 0 ; i < n ; i++) {
for (int j = i + 1 ; j < n ; j++) {
if (players[i].score < players[j].score) {
struct player temp = players[j];
players[j] = players[i];
players[i] = temp;
}
}
}
printf("\nHighest to lowest:\n");
for (int i = 0 ; i < n ; i++)
printf("Player #%d: %d\n", players[i].no, players[i].score);
return 0;
}

Related

Print half left pyramid/triangle and half right pyramid/triangle together

Newb Here!
Trying some things with C Language,
I tried to print Half left triangle/pyramid and Half right triangle/pyramid together in one print.
heres the code!
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i) // outer loop
{
for (j = 1; j <= i; ++j) // inner loop
{
printf ("*"); // print the Star
}
printf ("\n");
}
{
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*"); // print the Star
}
printf ("\n");
}
}
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
int hollow_spacing = 2;
int left_padding = 10;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 0; i < rows; i++)
{
int curr_left_pad = left_padding + rows - i;
while(curr_left_pad-- > 0){
printf (" ");
}
for (int j = i; j >= 0; j--)
printf ("*");
for (int j = 0; j < hollow_spacing; ++j)
printf (" ");
for (int j = i; j >= 0; j--)
printf ("*");
printf ("\n");
}
}

I have to print a triangle of integers of collatz in c

I have to write a program in C which, prints numbers from collatz as a triangle.
Like this:
I have tried and
This is my output:
This is my code:
int inputLines, startNum, number;
printf("Lines: ");
scanf("%d",& inputLines);
printf("Start: ");
scanf("%d",& startNum);
for(int i = 0; i < inputLines; i++){
printf("\n");
for(int j = 0; j <= i; j++){
printf("%d ", startNum);
if(startNum % 2 == 0){
startNum = startNum / 2;
}else{
startNum = startNum * 3 + 1;
}
}
}
#include <stdio.h>
int main(void) {
int inputLines = 4;
int start = 19;
for(int i = 0; i < inputLines; i++)
{
int n = start;
for(int j=0; j<i+1; ++j)
{
printf("%d ", n);
n = (n%2)? 3*n+1 : n/2;
}
printf("\n");
}
return 0;
}

In C program which remove duplicates from an array, when I print an array, the output is not true

This program which written in C should remove duplicated elements from 2 input arrays by user, so when I print the second array which is b[z] after removing duplicates, the output is not true as it prints weird number instead of the input number by the user. (the problem in code is determined by comment).
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, z ;
printf("Enter size of array\n");
scanf("%d", &n);
int a[n];
if(n <= 20) {
for(int i = 0 ; i < n; i++) {
printf("Enter integer \n");
scanf("%d", &a[i]);
}
}
for(int i = 0 ; i < n; i++) {
printf("%d ", a[i]);
}
printf("\nEnter size of the 2nd array\n");
scanf("%d", &z);
int b[z];
if(z <= 20) {
for(int i = 0 ; i < z; i++) {
printf("Enter integer \n");
scanf("%d", &b[z]);
}
}
for(int i = 0 ; i < z; i++) {
printf("%d ", b[z]);
}
for(int i = 0 ; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i] == a[j]) {
for(int l = j; l < n; l++)
{
a[l] = a[l + 1];
}
n--;
j--;
}
}
}
printf("\nArray1: ");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
for(int t = 0; t < z; t++) {
for(int u = t + 1; u < z; u++) {
if(b[t] == b[u]) {
for(int l = u; l < z; l++)
{
b[l] = b[l + 1];
}
z--;
u--;
}
}
}
printf("\nArray2: ");
for(int e = 0; e < z; e++) {
printf("%d ", b[e]);
}
return 0;
}

Buble sort in c using array

I searched for bubble sort in C using array, stackoverflow provided me tyhe following code, which does not work on descending numbers(1,2,3,4,.....)
#include<stdio.h>
int main()
{
int numArray[5],temp,j;
printf("Please enter five integers:\n");
printf("First Number\n");
scanf("%d", &numArray[0]);
printf("Second Number\n");
scanf("%d", &numArray[1]);
printf("Third Number\n");
scanf("%d", &numArray[2]);
printf("Fourth Number\n");
scanf("%d", &numArray[3]);
printf("Fifth Number\n");
scanf("%d", &numArray[4]);
for (j=0; j<=5; ++j) {
if (numArray[j] > numArray[j+1]) {
temp = numArray[j];
numArray[j] = numArray[j+1];
numArray[j+1] = temp;
}
}
for(int j = 0; j < 5; j++) {
printf("%d ", numArray[j]);
}
return 0;
}
Update
Thank you, using another for loop, I made my ascending numbers bubble sorting code
int main()
{
int ara[5]={12,16,14,11,10};
int i,j,temp;
for(i=0;i<5;i++)
{
for(j=0;j<=5;j++)
{
if(ara[j]>ara[j+1])
{
temp=ara[j];
ara[j]=ara[j+1];
ara[j+1]=temp;
}
}
}
for(j=0;j<5;j++)
printf("%d\n",ara[j]);
}
try this :
for (i=0; i<5; i++) {
for (j=0; j<5; j++) {
if (numArray[i] > numArray[j]) {
temp = numArray[j];
numArray[j] = numArray[i];
numArray[i] = temp;
}
}
}
You initialize your array to have length 5 int numArray[5]
Hence, you only have indices 0,1,2,3,4 available.
Thus your loop for (j=0; j<=5; ++j) will yield an index 5 which doesn't exist.
Please refer to the below given code , i have added logic to your code and its working fine now
#include <stdio.h>
int main()
{
int numArray[5],temp,j;
printf("Please enter five integers:\n");
printf("First Number\n");
scanf("%d", &numArray[0]);
printf("Second Number\n");
scanf("%d", &numArray[1]);
printf("Third Number\n");
scanf("%d", &numArray[2]);
printf("Fourth Number\n");
scanf("%d", &numArray[3]);
printf("Fifth Number\n");
scanf("%d", &numArray[4]);
int len = sizeof(numArray)/sizeof(numArray[0]);
printf("len of array %d \n", len);
int i=0;
for(i = 0;i<len;i++)
{
for (j=0; j<len-i-1; j++) {
if (numArray[j] > numArray[j+1]) {
temp = numArray[j];
numArray[j] = numArray[j+1];
numArray[j+1] = temp;
}
}
}
for(j = 0; j < 5; j++) {
printf("%d ", numArray[j]);
}
return 0;
}
int swaped = 1;
while(swaped){
swaped =0;
for (j=0; j<5-1,; ++j) {//-1 for (j+1) upper bound is 4
//descending order : When the order of reverse exchange
if (numArray[j] < numArray[j+1]) {
temp = numArray[j];
numArray[j] = numArray[j+1];
numArray[j+1] = temp;
swaped = 1;
}
}
}
I think the more efficient way to do this is,
for (i=0; i<4; i++) {
for (j=i+1; j<5; j++) {
if (numArray[i] > numArray[j]) {
temp = numArray[j];
numArray[j] = numArray[i];
numArray[i] = temp;
}
}
}

Bubble Sort in C

I am trying to implement Bubble sort in C and have come this far but its nor sorting properly.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n+1; ++j)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
Input
2
12
1
13
Output
2
1
12
13
What am I missing ?
So now that the drama is behind us, the problem with your code was that you were not using the proper index in your inner loop. In addition your inner loop counter's conditional check was not correct. Also, as I mentioned in a comment to your question, you have a flaw in your code (which I have not fixed) where you initialize your array prior to asking the user how many elements they want to enter. This can lead to an index out of bounds exception if the user enters a number greater than 5.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
your second loop is not proper.
for(j=0;j<n-i-1;j++){
}
/*By your approach , in inner loop you are not checking the each elements . So change i to j in swapping , and limit of j should be till n-1*/
#include<stdio.h>
int main()
{
int n, i, j, a[10], b, temp=0;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n-1; ++j) // notice limit , also complexity can be reduced by changing to(j<n-i-1)
{
if(a[j] > a[j+1])
{
temp = a[j]; // changed variable
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
/*You May check this code it will help you*/
#include<stdio.h>
void bubble_sort(int a[],int n);
void bubble_sort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i<n;i++)
{
for (j=0; j<=n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
main()
{
int a[10];
int i,n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(a[j] > a[j+1]) //change the varible instead of i to j
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
I've tried to cover all possible conditions to reduce passes and comparisons for bubble sort to reduce the overall time taken. Here goes my code...
#include <stdio.h>
#include <conio.h>
void bubbleSort(int n){
int arr[n],i,j,temp=0;
int swapFlag = 0;
printf("\nInsert %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Insert complete.\n\n");
printf("Your array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
//Bubble Sort Algorithm
for(i=0;i<n-1;i++){
swapFlag = 0;
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swapFlag = 1;
//Swapping unordered pairs
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
//Condition to reduce number of passes & comparisons
if(swapFlag == 0){
break;
}
}
printf("\n\nAfter sorting the array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
void main(){
int n;
printf("Enter number of array elements: ");
scanf("%d",&n);
bubbleSort(n);
getch();
}
Result:-

Resources