Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
#include <stdio.h>
int main () {
int []={1,2,3,7,8}; // add element after 3 --> 4,5,6 (condition that i don't know position of 3 in array)
for(int i=0,i<10;i++)
{
printf("%d\n",n[i]);
}
return 0;
}
i want output 1,2,3,4,5,6,7,8
but remember in case i don't know the position of 3 or 7 in array
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
// n = len of tab
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
int main()
{
int len = 10;
int top_number_count = 3;
int data[10] = {1, 11, 3, 4, 5, 20, 7, 8, 9, 10};
int temp[10];
// copy the tab for dont change any value
for (int i = 0; i < len; i++) {
temp[i] = data[i];
}
// sort the new tab
bubbleSort(temp, len);
// print the top number
// top_number_count is the count of max number you want
for (int i = len - top_number_count; i < len; i++)
printf(">%d\n", temp[i]);
return 0;
}
As the number of large values to be displayed may vary, sorting the array is the best choice. There are different techniques of sorting. For this program I shall use the "bubble sort"(if you wish to learn other sorting techniques, check this).
Once the array is sorted in descending order(largest to smallest), we can specify the number of large values we need. The program below displays the first 3 largest numbers.
#include <stdio.h>
int main()
{
int data[10] = {0, 12, 90, 8, 1, 2, 7, 9, 11, 10} ;
int MaxElements = 10 ;
int endBoundary = MaxElements - 1 ;
for(int index = 0 ; index <= MaxElements-1 ; index++)
{
for(int counter = 0 ; counter < endBoundary ; counter++)
{
if(data[counter] < data[counter+1]) // then swap the values
{
int temp = data[counter] ;
data[counter] = data[counter+1] ;
data[counter+1] = temp ;
}
}
endBoundary-- ;
}
// displaying the first 3 largest numbers
int numOfValuesSought = 3 ;
for(int count = 0 ; count < numOfValuesSought ; count++)
{
printf("%d\n", data[count]) ;
}
return 0 ;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 months ago.
Improve this question
int temp = 0;
int counter;
int match = 0;
int FindDup(int array[], int K, int N)
{
// Sorting the array from small big numbers.
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
// Find minimum number of K occurences
for (int i = 0; i < N; i++)
{
counter = 0;
for (int j = 0; j < N; j++)
{
if (array[i] == array[j]) // checks if array element is equal
{
counter++;
}
}
if (counter == K)
{
match = array[i];
return match;
}
}
return -1;
}
Info about this function:
This function is a mix of sorting and finding minimum occurrences of an array.
Problem:
The function does work as intended but needs optimization from another user for further improvements of the code. It would be great seeing someones opinions of what would be better and what could be changed.
Input:
arraySize: 10
arrayElements: 2 4 6 7 3 4 5 6 3 6
numberOfOccurrences: 2
Output:
3
since the input is just integer array and there is ain't no difference between saying there is 2 and another 2 as they all the same , you could go up with a hashing techniques , there is a lot of hashing techniques out there , just for a little demonstration , assume that the range of the numbers entered is so small , like from 0 -> 45 and all is positive , then you can use hashing . but if there is negative numbers then you have to use other techniques , if the array is like 0 1 2 5 3000 then this will create array of size 3001 if you used direct hashing techniques , but to solve this problem , there is other hashing techniques that could help you.
for example , you could do something like this :
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[8] = {1, 10, 5, 5, 10, 4, 8, 11};
int max = arr[0];
/*get the maximum element in the array*/
for(int i = 1; i < 8; i++)
{
if(arr[i] > max)
max = arr[i];
}
/*create another variable of size = (max of arr) + 1 and initialized with zeros*/
int *hashTable = (int*) calloc((max + 1), sizeof(int));
/*hashing all the elements*/
for (int i = 0; i < 8; ++i) {
hashTable[arr[i]]++;
}
/*printing the sorted array*/
for (int i = 0; i < (max+1);) {
if(hashTable[i] > 0)
{
printf("%d\t", i);
hashTable[i]--;
}
else
{
i++;
}
}
return 0;
}
and this is the output:
1 4 5 5 8 10 10 11
here , using hashing , you can know the number of occurrence of each element and sort the array in time complexity O(N) .
so simply , read about hashing techniques . that could help answering your question
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I got this problem statement from a programming puzzle book. I am able to find out the horizontal palindromes, but only if the entire row is a palindrome.
How can I fulfill this? Also how can be diagonal palindromes acquired?
A pseudo code is okay too, I just need the basic logic behind this. I will perform the rest.
Thank You.
The trick behind finding the horizontal palindromes is the take an entire row and then split it into various strings. Once that is done, you need to check if that string is a palindrome. For vertical strings, you need to do the same thing for the columns.
Now for the diagonal ones, you need to start from a point at the edge and then move forward diagonally (+[1][1]) for going to the bottom right until you reach the end. Now keep doing for every tactical point of every edge which will help you get all the diagonal strings, the next thing you need to do is to split these strings and check if each of these short strings are a palindrome or not.
This would come under dynamic programming most likely. Although I am confused it might come under greedy approach as well. I'll confirm it once with my professor.
Here is the code I did back when I was trying to solve the same thing -
#define PALLEN 2
#include <stdio.h>
#include <string.h>
int a[10][10];
/*int a[5][5] = {
{ 1, 2, 1, 3, 5 } ,
{ 4, 5, 6, 7, 4 } ,
{ 4, 5, 5, 4, 1 } ,
{ 1, 9, 2, 1, 4 } ,
{ 1, 9, 4, 1, 5 }
};*/
int n=0;
void checkPalindrome(char*);
void diagonalPal();
void stringSpliter(char*);
int main() {
int i, j, k, l, x;
int c = 0;
int jmp;
int ptr = 0;
int diag;
char recycler[20];
char diaglist[25];
char revdiaglist[25];
system("cls");
printf("\nEnter the dimension (n) of this square matrix i.e. (n*n) - ");
scanf("%d", &n);
printf("\nNow enter the elements for this %d*%d matrix - ", n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("-%d-", a[i][j]);
}
printf("\n");
}
printf("\nHorizontal Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[i][jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nVertical Palindromes");
for (i = 0; i < n; i++) {
for (j = n-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp][i]; //0,0-- 1,0
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
printf("\n\nDiagonal Palindromes");
diagonalPal();
}
void stringSpliter(char *a){
int i,j,k,ptr,jmp,c=0,l;
int len;
len = strlen(a);
char recycler[20];
for (j = len-1, k = PALLEN; j > 0; j--, k++) {
while (c < j) {
jmp = c;
memset(recycler, 0, 20);
ptr = 0;
for (l = 0; l < k; l++) {
recycler[ptr] = a[jmp]; //0,0 -- 0,1
ptr++;
jmp++;
}
checkPalindrome(recycler);
c++;
}
c = 0;
}
}
void diagonalPal(){
int i, x=0, j, k, ptr=0;
char diagrecycler[20];
for(i = 0; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = i, k = 0; j < n, k < n; j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
for(i = 1; i < n; i++){
memset(diagrecycler, 0, 25);
ptr = 0;
for(j = 0, k = i; j < n, k < n ;j++, k++){
diagrecycler[ptr++] = a[j][k];
}
stringSpliter(diagrecycler);
}
}
void checkPalindrome(char *string){
int isPalindrome = 1, i=0;
char rev[20];
strcpy(rev, string);
strrev(rev);
isPalindrome = strcmp(rev, string);
if(isPalindrome == 0){
printf("\n");
while(string[i]!='\0') printf("%d", string[i++]);
}
}
// Output
/*Enter the dimension (n) of this square matrix i.e. (n*n) - 4
Now enter the elements for this 4*4 matrix - 1 2 3 4
5 2 1 6
8 1 1 8
9 5 3 2
-1--2--3--4-
-5--2--1--6-
-8--1--1--8-
-9--5--3--2-
Horizontal Palindromes
11
8118
Vertical Palindromes
22
11
3113
Diagonal Palindromes
121
212
G:\Code snippets\C programmes>*/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array with some 0's and 1's.Now I want to find how much time 1 comes successively(the largest value). Like I have an array of 0 1 1 1 0 0 1 1 1 1 0 1.Then my result will be 4. I can count the total occurrence of 1 but can't keep the successive occurrence. How can I keep the result of successive occurance.I am beginner
Each time you encounter a 0 you need to reset the counter. Before resetting however you need to check if the value is greater than the previous value you had for your counter.
I don't quite remember C syntax 100%, but something like this:
int size = sizeof(yourArray) / sizeof(int);
int largest = 0;
int counter = 0;
for(int i = 0; i < size; i++) {
if(yourArray[i] == 1) {
counter++;
if(counter > largest) {
largest = counter;
}
} else {
counter = 0;
}
}
This should work I guess
#include <stdio.h>
void main ()
{
int a[10] = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1};
int count = 0;
int temp = 0;
for (int i = 0; i < 10; i++)
{
if (a[i] == 1)
temp++;
else
{
if (count < temp)
{
count = temp;
temp = 0;
}
}
}
if (count < temp)
{
count = temp;
temp = 0;
}
printf (" Total %d", count);
}
Just use simple backtracking:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char const *argv[]) {
int array[] = {0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1};
int size, i, maxSoFar = 0, maxEndingHere = 0;
size = sizeof(array) / sizeof(*array);
for (i = 0; i < size; i++) {
if (array[i] == 1) {
maxEndingHere++;
} else {
maxEndingHere = 0;
}
if (maxEndingHere > maxSoFar) {
maxSoFar = maxEndingHere;
}
}
printf("%d", maxSoFar);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include <stdio.h>
int main () {
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
So I have this code so far, it writes every single element on the screen, but I would also like to write the highest number and its index. I'm only a beginner, so I'd like to ask your opinion.
It is nice to get effort (and code) with a question. Good job. All you need to do is compare each array value and save the largest as max. Note, whenever searching for a maximum value, it is good practice to initialize your max value as the minimum for that storage type ('int') in this case. So initializing to INT_MIN insures that any value (even a large negative one if all your values are negative) will be larger than the initial value. To capture the indexes where the maximum value occurs, simply save the index values for i, j each time you save a max value and you will have captured the indexes where the maximum value occurrs:
#include <stdio.h>
#include <limits.h>
int main () {
int a[][2] = {{0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int maxrow = -1, maxcol = -1;
int nrows = sizeof a/sizeof *a;
int i, j, max = INT_MIN;
for (i = 0; i < nrows; i++) {
for (j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
/* find largess value */
if (a[i][j] > max) {
max = a[i][j];
maxrow = i;
maxcol = j;
}
}
}
printf ("\n maximum value at 'a[%d][%d]' : %d\n\n",
maxrow, maxcol, max);
return 0;
}
Compile
gcc -Wall -Wextra -O3 -o bin/array_max array_max.c
Output
$ ./bin/array_max
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
maximum value at 'a[4][1]' : 8
In C function main without parameters shall be declared like
int main ( void )
As for the searching of the maximum element then the approach is simple. At first it is supposed that the maximum element is the first element of the array. Then all other elements of the array are compared with this maximum and if some element is greater than the maximum then it becomes the maximum.
For example
#include <stdio.h>
#define M 5
#define N 2
int main ( void )
{
int a[M][N] =
{
{ 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 }
};
int i, j;
int max_i, max_j;
for ( i = 0; i < M; i++ )
{
for ( j = 0; j < N; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
max_i = 0;
max_j = 0;
for ( i = 0; i < M; i++ )
{
for ( j = 0; j < N; j++ )
{
if ( a[max_i][max_j] < a[i][j] )
{
max_i = i;
max_j = j;
}
}
}
printf( "The maximum value is %d at row %d and column %d\n",
a[max_i][max_j], max_i, max_j );
return 0;
}
you can use the following code:
int max,pi,pj;
max = –2147483648;
for(i=0; i<5; i++) {
for(j=0; j<2; j++) {
if(a[i][j] > max) {
max = a[i][j];
pi = i;
pj = j;
}
}
}
Here's a loop to sort an array from min to max, I need the result of this loop to be put into another array so I can filter and remove the numbers that occur only once and find the last member of what's left.
Here's the code I have so far:
#include<stdio.h>
#include<conio.h>
#define buffas 1024
void main() {
int arr[buffas],i,j,element,no,temp;
printf("\nEnter the no of Elements: ");
scanf("%d", &no);
for(i=0; i<no; i++) {
printf("\n Enter Element %d: ", i+1);
scanf("%d",&arr[i]);
}
for(i=0; i<no; i++) {
for(j=i; j<no; j++) {
if(arr[i] > arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted array:");
for(i=0; i<no; i++) {
printf("\t%d",arr[i]);
}
getch();
}
How do I change the
printf("\t%d",arr[i]);
To fill another array and then sort that to remove single entries and leave ony those that repeat at least once.
eg. the first aray is
2 2 1 6 9 9
and after the second sorting the result should be
2 2 9 9
#include <stdio.h>
#define buffas 16
int main(void)
{
/* Instead of original input and sorting code */
int arr[] = { 1, 2, 2, 6, 9, 9, 10, 10, 10, 11, 12, 13, 14 };
int no = sizeof(arr) / sizeof(arr[0]);
/* Code to copy only duplicated elements in arr */
int copy[buffas];
int n = 0;
for (int i = 0; i < no; i++)
{
int j;
for (j = i + 1; j < no; j++)
{
if (arr[i] != arr[j])
break;
}
if (j - i > 1)
{
for (int k = i; k < j; k++)
copy[n++] = arr[k];
i = j - 1;
}
}
/* Print results for verification */
for (int i = 0; i < n; i++)
printf("c[%d] = %d\n", i, copy[i]);
return 0;
}
The code has been run with various lengths of sorted array and different data in the array; it seems to be correct. The code above produces the output:
c[0] = 2
c[1] = 2
c[2] = 9
c[3] = 9
c[4] = 10
c[5] = 10
c[6] = 10
Note that the code uses the C99 feature of declaring variables in a for loop control statement; if you're on Windows and without C99 support, you'll need to declare i and k outside the loops. If you're using GCC, you need to add -std=c99 or a similar option.