I am writing a program that arranges an array in nondecreasing order; then, it inserts a value into the sequence. I can easily get numbers in the beginning and the middle of the array, but whenever I add a number that should go at the end, I keep getting 0. Where am I going wrong?
#include <stdio.h>
int main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted X:\n");
scanf("%d", &key);
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is:\n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
If the number to be entered is larger than all elements the following loop poses an issue.
...
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
If the number is largest,then the pos will be junk and not n.The default value of an array is junk.
Replace it with this.
for (i = 0;i<n ; i++)
{
if (key < array[i])
{
break;
}
}
pos = i;
...
Related
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[100] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
My code for sorting an array in ascending order works properly. And it doesn't have any error but when I am changed the array size then the code doesn't work properly and has an error called stack smashing detected. What causes this problem?
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
Neither int arr1[100] = {}; nor int arr1[] = {}; is valid C code.
The program compiles because your compiler implements GNU extensions that allow empty initializers and zero length arrays.
The reason your program no longer works when you remove the length 100 is the array becomes too short for the elements you try and store into it.
You probably meant to write int arr1[n] = {}; which does not compile because VLAs (variable sized arrays) cannot have an initializer.
Here is a modified version:
#include <stdio.h>
int main() {
int n, i, j, k, l;
printf("Enter how many element on the array : ");
if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "invalid size\n");
return 1;
}
int arr1[n];
for (i = 0; i < n; i++) {
if (scanf("%d", &arr1[i]) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
int temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d%c", arr1[i], "\t\n"[i == n - 1]);
}
return 0;
}
I want to store elements of maximum and minimum frequency in the arr2 array if there are more than one element of same frequency then both the elements should be stored ? But it is showing wrong results and i am not able to find what is the err. Can anyone help me with this. Any kind of help would be greatly appreciated.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int arr2[n];
int prevcount = 0;
int k = 0;
// for finding max element
for (int i = 0; i < n; i++)
{
int count = 0;
//counting the number of times it has occured
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit;
}
}
//it will update the kth element if the count is greater than the prev count
if (prevcount < count)
{
arr2[k] = arr[i];
}
//if these both are same but the number is different then will iterate k by 1 and store that element as well
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit:
}
// for finding min element
prevcount = 1000;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array if there is then go to the next iteration
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit2;
}
}
if (prevcount > count)
{
arr2[k] = arr[i];
}
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit2:
}
for (int i = 0; i < k; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
As #SparKot suggests, sorting the array makes the problem simple. Would you please try:
#include <stdio.h>
#include <stdlib.h>
// compare values numerically
int numeric(const void *a, const void *b)
{
return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b);
}
int main()
{
int n, i, j;
int *arr; // input array
int *count; // count frequency: initialized to 0's by calloc
int min = 0; // minimum occurrences
int max = 0; // maximum occurrences
scanf("%d", &n);
if (NULL == (arr = malloc(n * sizeof(int)))) {
perror("malloc");
exit(1);
}
if (NULL == (count = calloc(n, sizeof(int)))) {
perror("calloc");
exit(1);
}
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), numeric);
// count the length of sequence of the same numbers
for (i = 0; i < n; i++) {
for (j = 0; i + j < n && arr[i] == arr[i + j]; j++) {
;
}
count[i] = j; // i'th element has length j
i += j - 1; // jump to next number
}
// find minimum and maximum frequencies
for (i = 0; i < n; i++) {
if (count[i]) {
if (min == 0 || count[i] < min) min = count[i];
if (max == 0 || count[i] > max) max = count[i];
}
}
// report the result
for (i = 0; i < n; i++) {
if (count[i] == min) {
printf("min frequency %d value %d\n", count[i], arr[i]);
}
if (count[i] == max) {
printf("max frequency %d value %d\n", count[i], arr[i]);
}
}
return 0;
}
Sample input (n=10):
6
1
2
5
1
2
3
1
3
6
Output:
max frequency 3 value 1
min frequency 1 value 5
I would like to know how to find duplicate values in the 1st row of my 2d array.
I thought that by setting array[0][0] == array[i][j], it would check if the array[0][0] equals to the number of array[0][rest of the column]. But my code is just popping up my try again message whenever I put my first value.
Here's what I've tried so far.
void main(void)
{
int array[2][5];
int i, j, l, k;
printf("\Please enter 10 values\n");
for (j = 0; j < 5; j++)
{
for (i = 0; i < 2; i++)
{
scanf("%i", &array[i][j]);
for (k = 0; k < 2; k++)
{
for (l = 0; l < 5; l++)
{
while (array[0][0] == array[i][j])
{
printf("You entered 2 identical numbers in the first row, try again:\n");
scanf("%i", &array[i][j]);
}
}
}
}
}
}
// this isn't the fastest algorithm but it works because of the small length
int check_duplicates(int arr[], int len) {
// iterate through the array
for (int i = 0; i < len; i++) {
// only need to check to the right
// since the left elements have been checked previously
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
// there's a duplicate, return
return 1;
}
}
}
// no duplicates found
return 0;
}
int main(void) {
int array[2][5];
int i, j, l, k;
printf("Please enter 10 values\n");
for (j = 0; j < 5; j++) {
for (i = 0; i < 2; i++) {
scanf("%i", &array[i][j]);
// a duplicate has been found
if (check_duplicates(array[0], j + 1)) {
printf("You entered a duplicate, try again.\n");
// undo one loop to read back into that position
i --;
}
}
}
return 0;
}
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;
}
The part that is commented 'recurring characters code' is a bit flawed as later on when I try to print the letter and number of occurrences, the letter is correct, but the number of occurrences is some random number. Can somebody explain where my logic is faulty?
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int main()
{
//declare variables
char arr[MAX], ch;
int counter1 = 0, counter2 = 0, i, j, temp, mostcommon[128], x = 0, y = 0;
//ask for user input until array reaches MAX or is interrupted by the 'X' character and append arr[i]
for(i = 0; i < MAX; i++)
{
printf("Input %d. character into array: ", i + 1);
ch = getchar();
if (ch == 'X'){
break;
}
getchar();
arr[i] = ch;
counter1++;
}
//recurring characters code
for (i = 0; i < 128; i++){
mostcommon[i] = 0;
}
x = mostcommon[0];
y = 0;
for (i = 0; i < counter1; i++)
{
mostcommon[(int) arr[i]] += i;
}
for (i = 0; i < 128; i++){
if (x < mostcommon[i]){
x = mostcommon[i];
y = i;
}
}
//print array as it was appended
printf ("\nArray:");
for (i = 0; i < counter1; i++)
{
printf("\narray[%d] = %c", i, arr[i]);
}
//sort array by descending ASCII value
for(i = 0; i < counter1 - 1; i++)
{
for(j = i + 1; j < counter1; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//print sorted array
printf ("\n\nSorted array:");
for (i = 0; i < counter1; i++)
{
printf("\narray[%d] = %c", i, arr[i]);
}
//print array without reoccuring characters
printf ("\n\nFinal array:");
for (i = 0; i < counter1; i++)
{
if (arr[i] != arr[i-1]){
printf("\narray[%d] = %c", counter2, arr[i]);
counter2++;
}
}
printf("\n\nThe most common character is %c and it recurred %d times", y, x);
return 0;
}
One note before:
*Shouldn't you raise counter1 at the start of the loop ? Because if you don't - when X is entered it won't raise counter1 and I supposed you want number of characters entered.
Now to the code - first in the loop:
for (i = 0; i < counter1; i++)
{
mostcommon[(int) arr[i]] += i;
}
What was your purpose? If you want the number of occurrences of the character then the loop should look like this:
for(i = 0 ; i < counter1 ; i++)
{
mostcommon[(int)arr[i]]++;
}
as "+= i" has no purpose according to my understanding - so it would be kind of random as you describe. Hope it helped.