This is the simplest insertion sort program.
Unfortunately, it doesn't provide an outcome:
it does prompt the user for the size of the array
and for the list of numbers, but it doesn't do
the sort. I would be grateful for your help!
/** Insertion sort **/
#include <stdio.h>
int main (void)
{
int size, array[80], i, j, element;
printf("Enter number of elements: \n");
scanf ("%d", &size);
printf("Enter %d integers\n", size);
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
for (i = 0; i < size; i++)
{
element = array[i];
j = i;
while (j > 0 && array[j-1] > element)
{
array[j] = array[j-1];
array[j-1] = element;
j--;
}
}
printf ("Sorted list in ascending order:\n");
for (i = 0; i < size; i++)
printf ("%d", array[i]);
return 0;
}
This is one of those confusing moments :)
I have forgotten that there is a '\n' character
in the second printf, after which the user is
prompted for the list. Therefore, I've kept typing
in numbers in one row, without hitting ENTER; of course,
the program considered that as one number, only one integer
not the list ))) Thank you everyone for replying.
Sorry for this.
Related
I was having a trouble regarding to printing out the largest number in array in c language and i don't know how to do it while using loop. Please help me thanks
here is my code
#include <stdio.h>
void main() {
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for (int i = 0; i < size; i++) {
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for (int i = size - 1; i >= 0; i--) {
printf("%d ", nums[i]);
}
for (int a = 0; a < size; a++) {
for (int i = 0; i < size; i++) {
if (nums[a] > nums[i]) {
Lnum = nums[a];
}
}
}
printf("\nLargest element = %d", Lnum);
}
The algorithm for determining the largest element in an array goes as follows.
Use the value of the first array element as a start value for the biggest number.
Go over the rest of the array and check for each element if it is a bigger number.
When a bigger number is found use the bigger number for further comparisons.
#include<stdio.h>
void main(){
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for(int i=0; i<size; i++){
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for(int i=size-1; i>=0; i--){
printf("%d ", nums[i]);
}
Lnum = nums[0]; // Assume first element to be the largest element
for(int a=1; a<size; a++){ // start loop with second element
if(Lnum < nums[a]) { // check if current element is larger
Lnum = nums[a]; // found larger element, it is now the largest element of all inspected element
}
}
printf("\nLargest element = %d", Lnum);
}
Output:
Enter the size: 3
1 2 3
Reversed = 3 2 1
Largest element = 3
...Program finished with exit code 0
#include <stdio.h>
int main ()
{
int n, i;
int arr[n];
int max1 = 0;
int max2 = 0;
// introducing array elements
printf ("Enter n: ");
scanf ("%d", &n);
for (i = 0; i < n; i++)
{
scanf ("%d", &arr[i]);
}
// calculating max1, largest number
for (i = 0; i < n; i++)
{
if (arr[i] >= max1)
max1 = arr[i];
}
printf ("The first maximum is %d\n", max1);
// calculating max2, iterate the array again
for (i = 0; i < n; i++)
{
if ((arr[i] >= max2) && (arr[i] != max1))
max2 = arr[i];
}
printf ("The second max is %d", max2);
}
The problem asks me to read an one dimension array and find the two largest numbers in this array. I tried to solve it in a less efficient way, so I will first iterate the entire array in order to find the first maximum, then iterate it again to find the second one.
My error is segmentation fault (Core dumped) and I don't understand where I did wrong.
You're not allowed to set a stack allocated array using a variable at:
int arr[n];
You are allowed to do this:
int arr[10] //or any number
Basically the size of the array has to be known at compile time.
You've also never set actually set n to a number.
Edit: This apparently incorrect in modern c you are allowed to do this.
It should be like this
#include <stdio.h>
int main ()
{
int n, i;
int max1 = 0;
int max2 = 0;
printf ("Enter n: ");
scanf ("%d", &n);
int arr[n];
// introducing array elements
for (i = 0; i < n; i++)
{
scanf ("%d", &arr[i]);
}
// calculating max1, largest number
for (i = 0; i < n; i++)
{
if (arr[i] >= max1)
max1 = arr[i];
}
printf ("The first maximum is %d\n", max1);
// calculating max2, iterate the array again
for (i = 0; i < n; i++)
{
if ((arr[i] >= max2) && (arr[i] != max1))
max2 = arr[i];
}
printf ("The second max is %d", max2);
}
note this
printf ("Enter n: ");
scanf ("%d", &n);
int arr[n];
your compiler gives an arbitrary garbage value to n, in your code and that possibly is smaller than the number of iterations you have.
I,however, doubt the working of my code in some classic compilers as they want you to put all the declarations on the top
I have a one-dimensional array in the C language. I enter the number of elements (n) and then I enter the elements themselves. I want to find the position and the value of the first positive element and of the last negative element. How can I do it? I was thinking about something like this, but there's a semantic error and I'm stuck. I am just a beginner and I would really appreciate your help. Thank you in advance!
#include <stdio.h>
int main()
{
int Array[100], i, n;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (i = 0; i < n; i++)
{
printf("Enter number%d: ", i + 1);
scanf("%d", &Array[i]);
}
for (i = 0; i < n; i++)
{
if (Array[i] > 0)
{
printf("The first positive element is %d\n", Array[i]);
}
}
for (i = 100; i < n; i--)
{
if (Array[i] < 0)
{
printf("The last negative element is %d\n", Array[i]);
}
}
}
Test for first positive does not exit so you'll get all of them.
Test for negative needs to start at n-1 not 100 and terminate after it has tested element 0. It, too, needs to break when it's found one.
In the line:
for (i=100; i<n; i--)
you should write
for (i=(n-1); i>=0; i--)
that way you start from the last inserted element until you reach 0, the beggining of your array.
Furthermore in your if's you could use a break; command after your printf's. That way when you meet an element with the required characteristics you exit your loop. If you don't use break, you print every positive and negative element, not just the first you meet
Few problems:
in your 2nd loop - you don't break, meaning it will continue running (and printing!!) even after 1st positive was found
in your 3rd loop - i = 100; i < n. Wrong! it should have been i = n-1; i >= 0 since the array is only 100 elements long. And also the same problem with no break as before
INDENTATION!!!! (makes your code readable)
What happens if n > 100 ? (hint: you will insert elements to memory you don't own --> UB)
Here is a fixed version:
#include <stdio.h>
int main()
{
int Array[100], i, n;
printf("Enter n: ");
scanf("%d", &n);
if( n > 100)
{
printf("error\n");
return 1;
}
printf("Enter %d elements\n", n);
for(i=0; i<n; i++)
{
printf("Enter number%d: ",i+1);
scanf("%d", &Array[i]);
}
for(i=0; i<n; i++)
{
if (Array[i]>0)
{
printf("The first positive element is %d\n", Array[i]);
break;
}
}
for (i=n-1; i>=0; i--)
{
if (Array[i]<0)
{
printf("The first negative element is %d\n", Array[i]);
break;
}
}
return 0;
}
This is an example of a c program to sort a list of names... I'm new to algorithms that's why I need to know what type it is!
What are real life examples could I use it for too?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char *str[5], *temp;
int i, j, n;
printf("\nHow many names do you want to have?");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the name %d: ", i);
flushall();
gets(str[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (strcmp(str[j], str[j + 1]) > 0) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
flushall();
printf("\nSorted List : ");
for (i = 0; i < n; i++)
puts(str[i]);
return (0);
}
I hope its a bubble sorting. And with this you can arrange numbers in ascending or descending order.
First of all the program is invalid. It does not allocate memory where it is going to store strings. As result it is a very risky step to ask the user
how many names does he want to have because there is no space to store strings in the program.:)
You could use a two-dimensional variable length character array for the strings and standard function fgets instead of gets to enter the strings.
As for the sort algorithm then it is a bad realization of the bubble sort.:)
I am trying to learn C and I am trying to write a piece of code which does the following:
Take user input of a natural number n
Take user input of n elements and store them in the array x
Delete all negative numbers from the array x
Print the new array, with the length n - number of deleted elements
Here is my code:
#include <stdio.h>
int main(void)
{
int n, i, count=0;
double x[1000];
scanf("%d", &n);
for (i=0; i<n; i++)
scanf("%lg", &x[i]);
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
x[i-count]=x[i];
};
n -= count;
for (i=0; i<n; i++)
printf("%d: %g\n", i, x[i]);
return 0;
}
I have been told that I should replace my second for loop with the following code:
int j=0
...
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
if (i > j)
x[j] = x[i];
j++;
};
Could someone please explain why is the latter code better?
If i==j, then you're assigning an element to itself: not wrong, but a (small) waste of effort.
If you really want to improve this, avoid putting the negative values in the array in the first place.