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;
}
I have output in image, but code is not proper
i want code for the given output
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
The question was a little difficult to understand, I'm assuming you're asking how to get the output as described in the image. This code does that:
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i=0; i<rows; ++i) {
for(int j=0; j<rows*2-1; ++j) {
if (j <= i || j >= rows*2-2-i) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
I'm trying to multiply a number that i choose to a multidimensional array with values that i have chose, but my code doesn't do that.
Here it is the multiplication function:
int product_array(){
i = 0;
j = 0;
int number;
int product[i][j];
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
product[i][j] = number + product[0][0] * m_array[i][j];
j++;
}
i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
printf("\t%d ", product[i][j]);
j++;
}
i++;
printf("\n");
}
return product[i][j];
}
Legend: m_array means multidimensional array
Example of input ad output
Input
m_array values = 2 2 2 2
Number = 2
Output
4 4 4 4
To be clearer this is the piece of code with which the user can insert the values into the m_array:
do {
printf("How many rows will be in the array?\n");
scanf("%d", &righe);
} while (rows > Row || rows < 1);
do {
printf("How many columns will be in the array?\n");
scanf("%d", &columns);
} while (columns > Column || columns < 1);
printf("Data input\n");
while (i < rows) {
j=0;
while (j < columns) {
printf("Insert the element in the %d column and %d row\n", i, j);
scanf("%d", &m_array[i][j]);
j++;
}
i++;
}
Ok, so I figured out what the problem was (pretty simple), here is the code with the solution:
int product_array(){
i = 0;
j = 0;
int number;
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
m_array[i][j] = number * m_array[i][j];
j++;
}
i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
printf("\t%d ", m_array[i][j]);
j++;
}
i++;
printf("\n");
}
return m_array[i][j];
}
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:-
I need to sort an array of 5 numbers, this is what I have. I prompt for input, then use bubble sorting to sort the date, and then print the array. However when I run the program the output is not sorted and isn't even the same as the input.
void main() {
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]);
}
}
Your loop is invalid (it goes off the end by one), and your sort only contains one loop; a bubble sort contains two loops:
for (i = 0; i < SIZE-1; i++) {
for (j = i+1; j < SIZE; j++) {
if (val[i] > val[j]) swap(...);
}
}