How to append values to array in C language - c

given a set of 10 digit number i need to find how many times the index appears in the give number e.g
given 2 2 0 0 0 3 0 0 1 0 as an array element the array index from 0-9 so i have something like
0 1 2 3 4 5 6 7 8 9
2 2 0 0 0 3 0 0 1 0 ---> input
00115558 -----> output
the first index 0 appear twice, the second index 1 appear twice, the third index 0 appear 0 times so it's ignore and so on so the outcome is 00115558 and now i need to rearrange the output such that 0 must not start so i need something like 10015558
i was able to get the initial output like 00115558, but i need to save it into an array so i can loop through the array and check for value greater than 0 and swap the position.
int main() {
int i,j,len;
int array[10],output[10];
printf("Enter 10 digit number separated by space: \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
len = sizeof(array)/sizeof(array[0]);
for(i=0; i<len; i++)
{
if(array[i] != 0)
{
for(j=0; j<array[i]; j++)
//output[j] = i;
printf("%d ", i);
}
}
I tried to save the output into another array and i printed the array outside the loop to see the content but the result wasn't what i wanted. How can i save my value into the output[] array. thanks for any help

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,len;
int array[10],output[10];
printf("Enter 10 digit number separated by space: \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
len = sizeof(array)/sizeof(array[0]);
for(i=0; i<len; i++)
{
if(array[i] != 0)
{
for(j=0; j<array[i]; j++)
//output[j] = i;
printf("%d ", i);
}
}
}
Sir, i tried you exact code as above in "Dev c++". I get answer is 00115558. I think your Code is absolute Right. change your IDE software or Update that.

Related

Copy an array to another while sorting it simultaneously

I have already done the sorting part and the copy but I am really struggling to put it together.
Here is only the sorting part because i am not so familiar with it. My main problem is when I start copying the array it only copies the first 2 number then stops and I think its a small problem in the for loop somewhere but i cant find it.
int main ()
{
int number[30];
int number2[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The array is copied and sorted like:\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
system("Pause");
}
I compiled (MinGW) and ran this exact code and got the following output:
Enter the value of N
10
Enter the numbers
9
1
0
2
8
3
7
5
4
6
The array is copied and sorted like:
9
8
7
6
5
4
3
2
1
0
Press any key to continue . . .
It looks like it is correctly reverse-sorting the input values. Note that this is a fairly common sorting algorithm (https://www.geeksforgeeks.org/bubble-sort/).
Based on this I would argue that the for loop works as is, assuming that you are looking for a reverse sort.
I am not sure what exactly is meant by "copying the array". Array number2 is unused and there are no operations that appear to copy the array number, just operations for loading it, sorting it and printing it.

Need to write a program that asks the user to fill in a 2D array then checks how many times a digit between 0 and 9 was entered in those two rows

**This was a deleted question but I remade it so it easier for this community to understand what I'm asking
Here is my code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int row;
int col;
int x;
int y;
int i = 9;
int count[i];
printf("Enter the size of your array: ");
scanf("%d %d", &row, &col);
int arr[row][col];
//This will read the rows
for (int x = 0; x < row; ++x) {
for (int y = 0; y < col; ++y) {
printf("Enter row %d: ", x);
scanf("%d", &arr[x][y]);
}
}
//This will create a count for the rows
for (x = 0; x < row; ++x) {
for (y = 0; y < col; ++y) {
++count[arr[x][y]];
}
}
//This will count if digits repeat in the rows and print it
printf("\nTotal count for each digit:\n");
for (int j = 0; j < 10; ++j) {
printf("Digit %d occurs %d time%s\n", j, count[j], count[j] > 1 ? "s" : "");
}
return 0;
}
Notes on the code
I made i = 9 because the max number the user should enter is 9
On the "This will read the rows" there should be two printf's
"Enter Row 0"
"Enter Row 1"
How would I go and make it so the user would enter a set of numbers for the user to enter in both the rows. When I compile it just keeps saying "enter row 0: enter row 0: enter row 0". The program should find out how many times a number between 0 and 9 was entered. The final result should look like this
Enter the size of your array: 2 6
Enter row 0: 0 1 2 3 4 5
Enter row 1: 0 1 6 7 8 9
Total count for each digit:
Digit 0 occurs 2 times
Digit 1 occurs 2 times
Digit 2 occurs 1 time
Digit 3 occurs 1 time
ect. This would keep going until it the program hits "Digit 9 occurs however many times.
When I compile without the printf it runs through 3 rows when it should be 2 and most of the numbers that the compiler gives out are wack except for 2 digits
Ex The Digit 1 occurs 3 times
Digit 2 occurs -343589435 times
Thanks for any help!
There are ten digits 0-9. So the array count should have ten elements. Also the array need to be initialized.
Either use an array with the size specified by a constant expression and initialize it in a declaration like
#define N 10
//...
int count[N] = { 0 };
Or use a variable length array and initialize it using the function memset declared in the header <string.h>
For example
#include <string.h>
//...
int i = 10;
int count[i];
memset( count, 0, i * sizeof( int ) );
Otherwise if the array is not initialized it will have indeterminate values.

Program using scanf in a loop prints incorrect output

I don't know what's happening with this code.
#include<stdio.h>
int main()
{
int ii[5], i;
for (i=1; i<=5; i++)
{
scanf("%d", &ii[i]);
}
printf("----------------------\n");
for(i=1; i<=5; i++)
printf("%d\n", ii[i]);
return 0;
}
After compiling when I provide input as
1 2 3 4 5
then it prints as it is,
but when I provide input in reverse order:
5 4 3 2 1
it keeps on asking up to some more digits and after that it prints out some random digits from given set of input.
How can I fix this?
c uses 0 indexing that means that array indexes start at 0 not 1. A for loop over an array should look like this:
int array[ARRAY_LENGTH], i;
for (i = 0; i < ARRAY_LENGTH; ++i) {
This will ensure that i will go from 0 to ARRAY_LENGTH - 1 and will not go outside the bounds of your array.
These lines:
for(i=1; i<=5; i++)
printf("%d\n", ii[i]);
will Access element 5 of ii where the maximum index is 4. This will cause Undefined Behavior which is likely why you are seeing random numbers appear.

What needs to be modified to get desired result in this C code

I have written a small piece of code that would perform Run length encoding kind of stuff on 1-D array but still far from desired result.
main()
{
int a[8]={2,0,0,0,3,0,0,9};
int i,temp,ct=0,flag,m;
int found[90]={0};
for(i=0;i<=7;i++)
{
if(!a[i])
{
ct++;
if(!found[a[i]])
{
flag=i;
found[a[i]]=1;
}
}
}
a[flag]=ct;
m=ct;
for(i=0;i<m;i++)
{
printf("%d",a[i]);
}
}/* end of main*/
Now for above array i would like to have output something below
2 5 0 3 9
But with my piece of code am getting
2 5 0 0 3
Can I have any suggestion on that?
Shouldn't run length encoding turn 2,0,0,0,3,0,0,9 into 2 1 0 3 3 1 2 0 9 1?
1) The first thing I see is wrong is that you aren't looking at the entire array. You're using < to stop before 8, but also stopping at 7, so you only evaluate array items 0 - 6.
2) If ct stands for count it's never reset (ct=0 only on declaration). Also it's assignment is this: a[flag]= ct; which overwrites your original data. It basically tracks the value of i.
This is my version I've just put together:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int runningCount = 1; //because we start at array index 1 and not zero
for (i = 1; i <= SZ; i++) {
if (a[i - 1] == a[i]) //value same as one before it...
runningCount++;
else { // new value found. print last one, and the count of the last one.
printf("%d %d ", a[i - 1], runningCount);
runningCount = 1; //reset for next loop
}
}
return 0;
}
The output is 2 1 0 3 3 1 0 2 9 1
Ok based on the comment left below, your algorithm would actually look like this:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int zero_count = 0; //target zeros specifically...
for (i = 0; i < SZ; i++) {
if (a[i] == 0)
zero_count++;
}
//now write it out in a bizarre, unparsable format again...
for (i = 0; i < SZ; i++) {
if (a[i] != 0) //write out all non zero values
printf("%d ", a[i]);
if (i == 0) { //this says put the zero count after the first number was printed
printf("%d 0 ", zero_count); //inserting it into a strange place in the array
}
}
return 0;
}
which outputs: 2 5 0 3 9
You need a <= in your for loop:
for(i=0;i<=7;i++)
instead of
for(i=0;i< 7;i++)
Otherwise you miss the last element.
All you appear to be doing is (a) counting the number of times 0 occurs in the array, and (b) replacing the first occurrence of 0 with that count. It's not clear how this is meant to be a useful encoding.
In any case, you're not getting your desired result, at least in part, because you're only modifying one element of the array. I suspect what you want, or at least think you want, is to shift the non-zero elements of the array to the left as you encounter them.
What is the utility of compressing the array in the way you propose? Is some other piece of code going to have to reconstruct the original, and if so how do you expect to do so from your desired result?

Reading an array of integers and printing them out

I'm learning C on my own and doing a few exercises. The following code reads in an
array of integers from the user. The integers are printed out when the user types in a "0" or when the array is filled. Now the problem is the output. When I type in "0" after I have typed in 3 digits e.g. 1 2 3 the output is the following: 1 2 3 -858993460 -858993460. I am not sure why I get the value "-858993460" but I have already found a solution to avoid it. Now my question is what the values mean and if there is a smarter solution than mine which is presented below as comments.
#include <stdio.h>
#include <string.h>
#define arraylength 5
int main ()
{
//const int arraylength = 21; //alternative possibility to declare a constant
int input [arraylength] ;
int temp = 0;
//int imax = 0;
printf("Please type in a your digits: ");
for (int i = 0; i < arraylength; i++)
{
scanf("%d", &temp);
if ( temp !=0)
{
input[i]= temp;
//imax= i;
}
else
{
//imax= i;
break;
}
if (i < arraylength-1)
printf("Next: ");
}
for (int i =0; i < arraylength; i++ ) // switch arraylength with imax
{
printf("%d", input[i]);
}
getchar();
getchar();
getchar();
}
This happens because irrespective of when the 0 input is given you print all 5 numbers:
for (int i =0; i < arraylength; i++ )
To fix this you can print only the number(s) user entered before entering a 0 by running a loop from 0 to i:
for (int j =0; j < i; j++ )
Those 2 numbers are the garbage that was left in the memory locations for the last 2 parts of your array. You never initialise those when you only input 3 numbers, so when you go through and print all 5 elements in the array, it prints whatever garbage was in the memory.
You print all integers in array which is size of arraylength = 5. So you get 5 integers in output. As you didn't initialize array, you get uninitilized values as 4th and 5th elements of array. You can use memset(&input, 0, arraylength*sizeof(int)); to set initials values in array to 0.

Resources