I have an array 'a' inserted by the user as:
printf("insert N");
scanf ("%d", &n);
printf ("insert group of numbers");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
Then, from the original array, I have to make a sequence which will have all the entries of the original array but if an entry is coming multiple times consecutively, then instead of writing that number multiple times, only two numbers should be added to the sequence: first denoting the repeated number and the second denoting its frequency. Example:
Array : 1,3,4,1,1,1,1,6,7,1,1,1,1,1,1,9,3,2,5,6,1,2,1,1,1,1,1.
Sequence: 1,3,4,1,4,6,7,1,6,9,3,2,5,6,1,2,1,5
The first 3 numbers are written down as they are. Then '1' comes consecutively 4 times so we append 1,4 to the sequence and so on.
Actually i m confused how i can make i and j are on the same steps,
as far as i know i and j start from 0 then 1,2,3,4
but how can I make them start each in the same step so if a[i(that is 3 in the step)] = 10
so a[j( on step 3) is 10 too?
Thanks for the time! I appreciate!
Following code will do the job.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,x,j=0,n,*a,*b;
printf("insert N");
scanf ("%d", &n);
a=(int *)malloc(n*sizeof(int));
b=(int *)malloc(n*sizeof(int));
printf ("insert group of numbers");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for( i=0;i<n;++i)
{
x=0;
while((i+1)<n&&a[i]==a[i+1])
{
++x;
++i;
}
if(x>1)
{
b[j++]=a[i];
b[j++]=x+1;
}
else b[j++]=a[i];
}
for(i=0;i<j;++i)
printf("%d ",b[i]);
}
Please check whether this code will help or not, insted of arr_1 elemets you could use the scanf and load the variables
#include<stdio.h> int main() {
int arr_1[]={1,3,4,1,1,1,1,6,7,1,1,1,1,1,1,9,3,2,5,6,1,2,1,1,1,1,1};
int arr_2[27];
int arr_len;
int i,j,cntr=0;
int flag = 0;
arr_len=sizeof(arr_1)/sizeof(int);
for(i=0,j=0;i<arr_len;i++)
{
if(arr_1[i] == arr_1[i+1])
{
if(flag == 0)
{
arr_2[j] = arr_1[i];
j++;
}
flag = 1;
cntr++;
}
else
{
if(cntr > 0)
{
arr_2[j] = cntr+1;
cntr=0;
flag = 0;
}
else
{
arr_2[j]=arr_1[i];
}
j++;
}
}
for(i=0;i<arr_len;i++)
printf("%d ",arr_1[i]);
printf("\n");
for(i=0;i<j;i++)
printf("%d ",arr_2[i]);
getch(); }
int count=0, last=0, j=0, n, i;
printf("insert N");
scanf ("%d", &n);
int b[n];
printf ("insert group of numbers");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
if(a[i] == last){
count++;
}
else {
b[j] = last;
j++;
if(count != 0){
b[j] = count + 1;
j++;
count = 0;
}
last = a[i];
}
}
Rather than doing post processing as a few others have suggested you can process the input as it is received.
The if statement increments the count when a duplicate is encountered.
The else statement sets the second array to the last value (not current value) when the current value is different. If the last value was a duplicate, it adds the number of duplicates in the next if statement. After that, it updates the last variable.
Here is a demonstrative program that is based on using Variable Length Arrays (VLA). If you want you can change it such a way that instead of VLA(s) it used either arrays of fixed sizes or dynamically allocated arrays.
#include <stdio.h>
int main( void )
{
while ( 1 )
{
printf( "\nEnter array size: " );
size_t n = 0;
scanf( "%zu", &n );
if ( !n ) break;
int a[n];
printf( "Enter %zu elements of the array: ", n );
for ( size_t i = 0; i < n; i++ ) scanf( "%d", &a[i] );
printf( "\na[%zu] = { ", n );
for ( size_t i = 0; i < n; i++ ) printf( "%d, ", a[i] );
printf( "}\n" );
size_t m = 0;
size_t count;
for ( size_t i = 0; i < n; i += count )
{
count = 0;
while ( ++count + i < n && a[count + i] == a[i] );
m += count > 1 ? 2 : 1;
}
int b[m];
for ( size_t i = 0, j = 0; i < n; i += count )
{
count = 0;
while ( ++count + i < n && a[count + i] == a[i] );
b[j++] = a[i];
if ( count > 1 ) b[j++] = count;
}
printf( "b[%zu] = { ", m );
for ( size_t i = 0; i < m; i++ ) printf( "%d, ", b[i] );
printf( "}\n" );
}
}
The program might have the following output
Enter array size: 27
Enter 27 elements of the array: 1 3 4 1 1 1 1 6 7 1 1 1 1 1 1 9 3 2 5 6 1 2 1 1 1 1 1
a[27] = { 1, 3, 4, 1, 1, 1, 1, 6, 7, 1, 1, 1, 1, 1, 1, 9, 3, 2, 5, 6, 1, 2, 1, 1, 1, 1, 1, }
b[18] = { 1, 3, 4, 1, 4, 6, 7, 1, 6, 9, 3, 2, 5, 6, 1, 2, 1, 5, }
Enter array size: 6
Enter 6 elements of the array: 1 1 1 1 1 2
a[6] = { 1, 1, 1, 1, 1, 2, }
b[3] = { 1, 5, 2, }
Enter array size: 0
Related
Given a array x with n integer components, write functions that allow performing the following operation: Carry out the circular permutation of the given array
I tried to do without poiters for the code, as it ends up with only arrays
I think the problem is that the array in the pcircular is lost and can't pass the value to the write function
Note: for array > 6 it will not work
#include <stdio.h>
int ArraySize(){ // get the size of the array
int size;
printf("What's the array size? ");
scanf("%d", &size);
return size;
}
void readArray(int size, int array[]){ /*put the values in the first array*/
for (int i = 0; i < size; i++){
printf("What is the \033[1;36m%d\033[m value? ", i+1);
scanf("%d", &array[i]);
}
}
void pcircular(int size, int array[size][size]){ //Circular permutation function
for (int j = 0; j <= size; j++){
/* printf("("); */
for (int i = 0; i < size; i++){
if (i == size - 1){
array[j+1][0] = array[j][i];
/* printf("%d", array[j+1][0]); */
}
else{
array[j+1][i+1] = array[j][i];
/* printf("%d", array[j+1][i+1]);
printf(", "); */
}
}
/* printf(")"); */
}
}
void writeArray(int size, int array[size][size]){ //Write the Array
for (int i = 0; i <= size; i++){
printf("(");
for (int j = 0; j < size; j++){
/* printf("\ni = %d j = %d\n", i, j); */
printf("%d", array[i][j]);
if (j != size-1){
printf(", ");
}
}
printf(")");
}
}
void main(){
int size = ArraySize();
int list[size][size]; // create the array
readArray(size, list[0]);
pcircular(size, list);
writeArray(size, list);
}
Input:
What's the array size? 6
What is the 1 value? 1
What is the 2 value? 2
What is the 3 value? 3
What is the 4 value? 4
What is the 5 value? 5
What is the 6 value? 6
Expected Output:
(1, 2, 3, 4, 5, 6)(6, 1, 2, 3, 4, 5)(5, 6, 1, 2, 3, 4)(4, 5, 6, 1, 2, 3)(3, 4, 5, 6, 1, 2)(2, 3, 4, 5, 6, 1)(1, 2, 3, 4, 5, 6)
Real Output:
(
write your pcircular function as follow:
void pcircular(int size, int array[size][size]){ //Circular permutation
for (int j = 1; j <= size; j++){
for (int i = 0; i < size; i++){
array[j][i ] = array[j - 1][(i + 1) % size];
}
}
}
but the main problem is here:
int list[size + 1][size]; // create the array
since you are creating one more row than the number, indeed the last row is copy of the first row.
if you dont change the pcircular function, and use your version, you should as well add to second dimention too.
I have an assignment to make a function that sort array that going down and then going up (for example: 9, 8, 7, 6, 5, 7, 11, 13) to array that going down all the way (for example: 13, 11, 9, 8, 7, 7, 6, 5).
I wrote this in online compiler (programiz), but it just doesn't work for me.
#include <stdio.h>
#include <stdlib.h>
#define N 10
void sort_dec_inc(int a[], int n) {
int pivot, i, q = 0;
int c[n];
for (i=0; i<n; i++) {
c[i] = 0;
}
for (i=0; i<n-1; i++) {
if (a[i]<a[i+1]) {
pivot=i+1;
break;
}
}
if (pivot == n-1) {
return;
}
for (i=0; i<pivot && n>=pivot; q++) {
if (a[i]>=a[n]) {
c[q] = a[i];
i++;
}
else {
c[q] = a[n];
n--;
}
}
if (n==pivot) {
while(i<pivot) {
c[q] = a[i];
q++;
i++;
}
}
else {
while (n>=pivot) {
c[q] = a[n];
q++;
n--;
}
}
for(i=0; i<n; i++) {
a[i] = c[i];
}
}
int main()
{
int num_arr[N] = {9, 7, 5, 3, 1, 2, 4, 6, 8, 10};
sort_dec_inc(num_arr, N);
int i;
for(i = 0; i < N; i++) {
printf("%d ", num_arr[i]);
}
return 0;
}
output (most of the times) : 9 7 5 3 1 2 4 6 8 10
Sometimes the output is different, for example: (410878976 10 9 8 1 2 4 6 8 10 )
If someone can answer in simple code its the best, I don't understand yet all the options in C.
(I'm sorry if its a clumsy code, I'm new for this.)
thanks a lot!
Solution based on the comments below:
#include <stdio.h>
#include <stdlib.h>
#define N 10
void sort_dec_inc(int a[], int n) {
int left, i = 0;
int right = n-1;
int c[n];
while (i<n) {
if (a[left] >= a[right]) {
c[i] = a[left];
left++;
}
else {
c[i] = a[right];
right--;
}
i++;
}
for(i=0; i<n; i++) {
a[i] = c[i];
}
}
int main()
{
int num_arr[N] = {9, 7, 5, 3, 1, 2, 4, 6, 8, 10};
sort_dec_inc(num_arr, N);
int i;
for(i = 0; i < N; i++) {
printf("%d ", num_arr[i]);
}
return 0;
}
output: 10 9 8 7 5 5 4 3 2 1
For starters it is a bad idea to define an auxiliary variable length array. Such an approach is unsafe because the program can report that there is no enough memory to allocate the array.
At least this code snippet
for (i=0; i<n-1; i++) {
if (a[i]<a[i+1]) {
pivot=i+1;
break;
}
}
if (pivot == n-1) {
return;
}
contains a logical error.
Consider an array that contains only two elements { 1, 2 }. In this case a[0] is less than a[i+1] that is than a[1]. So the condition of the if statement
if (pivot == n-1) {
return;
}
evaluates to logical true and the function returns the control though the array was not sorted in the descending order. It stays unchanged.
Or consider this code snippet
if (a[i]>=a[n]) {
c[q] = a[i];
i++;
}
The expression a[n] accesses memory beyond the array that results in undefined behavior.
A straightforward approach is to use the method insertion sort.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
void insertion_sort( int a[], size_t n )
{
size_t i = 1;
while ( i < n && !( a[i-1] < a[i] ) ) i++;
for ( ; i < n; i++ )
{
size_t j = i;
while ( j != 0 && a[j-1] < a[i] ) --j;
if ( i != j )
{
int tmp = a[i];
memmove( a + j + 1, a + j, ( i - j ) * sizeof( int ) );
a[j] = tmp;
}
}
}
int main( void )
{
int a[] = { 9, 7, 5, 3, 1, 2, 4, 6, 8, 10 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
insertion_sort( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
The program output is
9 7 5 3 1 2 4 6 8 10
10 9 8 7 6 5 4 3 2 1
I would recommend a bubble sort.
It is an easy way to sort numbers in numerical order:
for (int i = 0; i < quant; i++)
{
for (int j = 0; j < quant-1; j++)
{
if (Array[j] < Array[j + 1])
{
int temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
}
quant is the quantity of numbers you want to sort. Don't forget to define an integer array with length quant for the sorting process.
To access the array, just use a for loop to return all the results.
I have a multidimensional array with 3 rows and 4 columns. The program should use reverseRow()function to reverse a specific row from an array. Like, let's say user's input is 2, then it should reverse second row and print it.
I have tried a swap method, but it didn't work. I also tried using pointers, but it didn't work as well. Can someone explain it for me? How do I reverse a specific row?
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int n = sizeof(arr) / sizeof(arr[0]);
void reverseRow(int low, int high)
{
if (low < high)
{
int temp = arr[low][0];
arr[low][0] = arr[high][0];
arr[high][0] = temp;
reverseRow(low + 1, high - 1);
for (int i = 0; i < n; i++)
for (int j = 3; i > 0; j--)
printf("%d ", arr[i][j]);
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
reverseRow(0, n - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
Best regards.
You're reversing the first column, not a user-selected row.
You're not passing the row number to the function.
The loop that prints the array is printing all the columns in reverse order, and it's using n as the number of rows, not columns. I've renamed the variables to be clearer and fixed the printing loop.
#include <stdio.h>
#include <stdlib.h>
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
void reverseRow(int rownum, int low, int high)
{
if (low < high)
{
int temp = arr[rownum][low];
arr[rownum][low] = arr[rownum][high];
arr[rownum][high] = temp;
reverseRow(rownum, low + 1, high - 1);
} else {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
}
void printMenu()
{
printf("\n");
printf("You can choose one of these services: \n");
printf("1. Get the elements of a specific row reversed \n");
printf("Please select one to try ");
int answer;
scanf("%d", &answer);
switch (answer)
{
case 1:
printf("Please select row number: ");
int row;
scanf("%d", &row);
if (row >= rows || row < 0) {
printf("Invalid row\n");
break;
}
reverseRow(row, 0, cols - 1);
break;
case 2:
printf("Bye!\n");
break;
default:
printf("please select carefully! \n");
break;
}
}
int main()
{
printMenu();
return 0;
}
What is the row of a two-dimensional array?
It is a one dimensional array.
So what you need is to write a function that reverses a one-dimensional array.
An iterative function can look the following way
void reverseRow( int *a, size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
int tmp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = tmp;
}
}
A recursive function can look the following way
void reverseRow( int *a, size_t n )
{
if ( !( n < 2 ) )
{
int tmp = a[0];
a[0] = a[n-1];
a[n-1] = tmp;
reverseRow( a + 1, n - 2 );
}
}
And either function is called like for example
reverseRow( arr[1], 4 );
that reverses the second row of the original array.
Or if the number of row (starting from 0) is stored in some variable as for example row then the function is called like
reverseRow( arr[row], 4 );
To output the reversed row you need to write a separate function.
Here is a demonstration program.
#include <stdio.h>
void reverseRowIterative( int *a, size_t n )
{
for (size_t i = 0; i < n / 2; i++)
{
int tmp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = tmp;
}
}
void reverseRowRecursive( int *a, size_t n )
{
if (!( n < 2 ))
{
int tmp = a[0];
a[0] = a[n - 1];
a[n - 1] = tmp;
reverseRowRecursive( a + 1, n - 2 );
}
}
void printRow( const int *a, size_t n )
{
for (size_t i = 0; i < n; i++)
{
printf( "%2d ", a[i] );
}
putchar( '\n' );
}
int main( void )
{
enum { M = 3, N = 4 };
int arr[M][N] =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
for ( size_t i = 0; i < M; i++ )
{
reverseRowIterative( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
for (size_t i = 0; i < M; i++)
{
reverseRowRecursive( arr[i], N );
printRow( arr[i], N );
}
putchar( '\n' );
}
The program output is
4 3 2 1
8 7 6 5
12 11 10 9
1 2 3 4
5 6 7 8
9 10 11 12
The question: write a program that gets a list of numbers ( lets say 4 numbers ) and an extra number and checks if the extra number equals to the multiplying of two numbers from the list. If yes return true else return false.
For example a number list is ( 2,4,8,16) an extra number is 32, the program checks if 32 is equal to two numbers of the numbers from the list, and return true, in this example it will, because 32 equals to, my solution is below but its not correct, any help appreciated
int i;
int b;
int listA[4] = {2, 4, 8, 10};
printf("Enter your Extra number value \n");
scanf("%d", &b);
for(i=0; i<4; i++){//?
if(listA[i] * listA[i+] == b){// i+1 ?
printf("True! \n");
}else{
printf("False \n");
}
All you need is to write two nested loops.
You could write a separate function.
I suppose that the original array (list) is not necessary ordered.
For example
#include <stdio.h>
int is_multiplied( const int a[], size_t n, int value )
{
int result = 0;
if ( !( n < 2 ) )
{
for ( size_t i = 0; !result && i < n - 1; i++ )
{
for ( size_t j = i + 1; !result && j < n; j++ )
{
result = a[i] * a[j] == value;
}
}
}
return result;
}
int main(void)
{
int a[] = { 2, 4 , 8 , 10 };
const size_t N = sizeof( a ) / sizeof( *a );
int value = 32;
printf( "%s\n", is_multiplied( a, N, value ) ? "true" : "false" );
return 0;
}
The program output is
true
Since your code doesn't even compile. Id imagine you didn't put a lot of time into this.
int main()
{
int i;
int b;
int listA[4] = {2, 4 , 8 , 10};
bool verified = false;
printf("Enter your Extra number value \n");
scanf("%d" ,&b);
for(i=0; i<4 ; i++ )
{
for(int j=0; j<4; j++ )
{
if(j==i)
{
break;
}
if(listA[i] * listA[j] == b)
{
printf("True! \n");
verified = true;
continue;
}
else
{
//printf("False \n");
}
}
}
if(!verified)
{
printf("False \n");
}
}
To start with approach these sort of problems in the easiest possible method. using nested for loop.
As mentioned in one of the comments add a nested for loop.
int i=0;
int j=0;
int b=0;
int listA[4] = {2, 4 , 8 , 10};
printf("Enter your Extra number value \n");
scanf("%d" ,&b);
for(i=0; i<4 ; i++){//?
for (j=i; j<4; j++) {
if(listA[i] * listA[j] == b)
{
printf("True! \n");
return 0;
}
}
}
printf("False \n");
return 0;
Further optimization:-
for(i=0; i<4 ; i++){
if ((i > b) || ((b % i) != 0))
continue;
k = b/i;
for (j=i; j<4; j++) {
if(listA[j] == k)
{
printf("True! \n");
return 0;
}
}
}
I should make new array out of existing one (ex. 1 0 4 5 4 3 1) so that the new one contains digits already in existing array and the number of their appearances.
So, the new one would look like this: 1 2 0 1 4 2 5 1 3 1 (1 appears 2 times, 0 appears 1 time.... 3 appears 1 time; the order in which they appear in first array should be kept in new one also); I know how to count no. of times a value appears in an array, but how do I insert the no.of appearances? (C language)
#include <stdio.h>
#define max 100
int main() {
int b, n, s, i, a[max], j, k;
printf("Enter the number of array elements:\n");
scanf("%d", &n);
if ((n > max) || (n <= 0)) exit();
printf("Enter the array:\n");
for (i = 0; i < n; i++)
scanf("%d", a[i]);
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (a[j] == a[i]) {
for (k = j; k < n; k++) {
a[k] = a[k + 1];
}}}}
//in the last 5 rows i've tried to compare elements, and if they are same, to increment the counter, and I've stopped here since I realised I don't know how to do that for every digit/integer that appears in array//
If you know that the existing array consists of digits between 0 and 9, then you can use the index of the array to indicate the value that you are incrementing.
int in[12] = {1,5,2,5,6,5,3,2,1,5,6,3};
int out[10] = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < 12; ++i)
{
++out[ in[i] ];
}
If you provide any code snippet, its easy for the community to help you.
Try this, even you optimize the no.of loops :)
#include <stdio.h>
void func(int in[], int in_length, int *out[], int *out_length) {
int temp[10] = {0}, i = 0, j = 0, value;
//scan the input
for(i=0; i< in_length; ++i) {
value = in[i];
if(value >= 0 && value <= 9) { //hope all the values are single digits
temp[value]++;
}
}
// Find no.of unique digits
int unique_digits = 0;
for(i = 0; i < 10; ++i) {
if(temp[i] > 0)
unique_digits++;
}
// Allocate memory for output
*out_length = 2 * unique_digits ;
printf("digits: %d out_length: %d \n",unique_digits, *out_length );
*out = malloc(2 * unique_digits * sizeof(int));
//Fill the output
for(i = 0, j = 0; i<in_length && j < *out_length; ++i) {
//printf("\n i:%d, j:%d val:%d cout:%d ", i, j, in[i], temp[in[i]] );
if(temp[in[i]] > 0 ) {
(*out)[j] = in[i];
(*out)[j+1] = temp[in[i]];
temp[in[i]] = 0; //Reset the occurrences of this digit, as we already pushed this digit into output
j += 2;
}
}
}
int main(void) {
int input[100] = {1, 0, 4, 5, 4, 3, 1};
int *output = NULL, output_length = 0, i = 0;
func(input, 7, &output, &output_length );
for(i=0; i < output_length; i+=2) {
printf("\n %d : %d ", output[i], output[i+1]);
}
return 0;
}