I'm writing a program that reads integers from keyboard input and find the occurrence then sort them in descending order.
I got the occurrence and the descending but when I type same integers, they are repeated if I type 8 7 8
it's like
8 2
8 2
7 1
help please??
This is my code
#pragma warning (disable :4996)
#include <stdio.h>
#include <stdlib.h>
int main(){
int input;
int inputarr[50], count[50] = {0};
int i=0, j;
int last = 0;
printf("Enter numbers \n");
///getting int
while (scanf("%d", &input) > 0)
{
inputarr[i] = input;
i++;
}
last = i;
printf(" N Count\n");
printf("----- -----\n");
int a;
/// increment count
for (i = 0; i < last; i++){
count[inputarr[i]] = count[inputarr[i]] + 1;
}
/////ascending
for (i = 0; i < last; i++)
{
for (j = 0; j < last; j++){
if (inputarr[j]<inputarr[j + 1])
{
int temp = inputarr[j];
inputarr[j] = inputarr[j + 1];
inputarr[j + 1] = temp;
}
}
printf(" %d %d\n", inputarr[i], count[inputarr[i]]);
}
return 0;
}
The problem with this code is that you are printing the array while it's being sorted. Inputing numbers 1 2 3 4 would result in numbers 2 1 4 1 2 1 1 1 being outputed. To accomplish what you wanted you should move the printing part out of the sorting loop. Even then when a number is appearing multiple times in the input the output won't contain it only once (ex. 8 7 8 -> 8 2 8 2 7 2). To do that you should not output the number if it is the same as the previous number you outputed.
Another thing, in the sorting loop you are potentially accessing non existing array elements at the line
for (j = 0; j < last; j++){
When j = last-1 you are accessing last element which might not exsist.
After fixing all this problems code might look like this:
for (i = 0; i < last; i++)
{
for (j = 0; j < last-1; j++){
if (inputarr[j]<inputarr[j + 1])
{
int temp = inputarr[j];
inputarr[j] = inputarr[j + 1];
inputarr[j + 1] = temp;
}
}
}
if (last == 0)
return 0;
printf(" %d %d\n", inputarr[0], count[inputarr[0]]);
for(i = 1; i < last; i++)
if (inputarr[i] != inputarr[i-1])
printf(" %d %d\n", inputarr[i], count[inputarr[i]]);
Related
So I am trying to print patterns in C.
For n = 2
Output:
2 2 2
2 1 2
2 2 2
for n = 3
Output:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
and so on.
My Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int nn = n;
int *arr;
arr = (int*)malloc(n*sizeof(int));
int f = 0; //flag to check if I reached the mid of the pattern
int l = 2*n-1; //Lenght of square to be generated
int temp1 = 0;
int temp2 = l;
for(int i = 0;i<l;i++)
{
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
for(int k = 0;k<l;k++)
{
printf("%d ",arr[k]);
}
printf("\n");
if(n == 1)
{
f = 1;
}
if(f==0) //For upper half of pattern
{
n=n-1;
temp1=temp1+1;
temp2=temp2-1;
}
else if(f==1) //For lower half of pattern
{
n=n+1;
temp1=temp1-1;
temp2=temp2+1;
}
}
return(0);
}
I am getting the correct output for n = 2 but when I am inputting anything above 2 the code is crashing.
I am not able to find what should be done. Can someone help me out and tell me what am I doing wrong?
It would be better to check distances by axis and take a maximum of them to be printed.
Something like:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int vertical_distance;
int horizontal_distance;
int n;
scanf("%d",&n);
for(int i = 0; i < n * 2 + 1; i++)
{
for(int j = 0; j < n * 2 + 1; j++)
{
vertical_distance = abs(n - i);
horizontal_distance = abs(n - j);
if (vertical_distance > horizontal_distance)
printf("%d ", vertical_distance);
else
printf("%d ", horizontal_distance);
}
printf("\n");
}
return(0);
}
Also, when I ran your code it worked nicely with large numbers (3, 7, 15).
I just pasted your code to onlinegdb.com/online_c_compiler and ran it. Can you please add the error message that you have got?
(sry for my English)
in this code piece
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
j can be bigger than n. but arr malloc space is n. the array overflowed.
it will work with a small change
arr = (int*)malloc(2*n*sizeof(int));
I want to print some C pattern for upper triagnle which sholud print numeric value 1 to 10.
Now my code is like this:-
#include <stdio.h>
int main() {
int j = 1, k, l, i;
for (i = 4; i >= 1; i--) {
for (k = i - 1; k >= 0; k--) {
printf(" ");
}
for (l = 4; l >= i; l--) {
printf("%2d", j);
j++;
}
printf("\n");
}
return 0;
}
Its output is fine but when my value reach near 10 it's space is missing.
How can I resolve it?
My output looks like
1
2 3
4 5 6
7 8 910
It should be:
1
2 3
4 5 6
7 8 9 10
You can change the alignment by using the - sign. This makes it left-justified.
Replace
printf("%2d",j);
with
printf("%-2d",j);
Live demo
If you want the bottom of the pyramid to be completely to the left with no space you can replace
for (k = i - 1; k >= 0; k--)
with
for (k = i - 1; k > 0; k--)
EDIT:
As #chqrlie pointed out this will leave a trailing blank space in every line except the last one, this can be fixed like:
//...
for (i = 4; i >= 1; i--)
{
for (k = i - 1; k > 0; k--) // change from k >= 0 to k > 0
{
printf(" ");
}
for (l = 4; l >= i; l--)
{
printf(" %d", j); // change from "%2d" to " %d"
j++;
}
printf("\n");
}
//...
Live demo
For this problem, with n lines of output, you need to produce n - i spaces at the beginning of line i and i numbers each preceded by a space. Instead of "%2d", use " %d" and output one less space at the start of each line. You can use printf to output an arbitrary number of spaces using %*s and an empty string.
Here is a modified version:
#include <stdio.h>
int main() {
int i; // line number
int j = 1; // starting number
int n = 4; // number of lines
int k;
for (i = 1; i <= n; i++) {
printf("%*s", n - i, ""); // output n - i spaces
for (k = 0; k < i; k++) {
printf(" %d", j);
j++;
}
printf("\n");
}
return 0;
}
Output:
1
2 3
4 5 6
7 8 9 10
It can be solved in two ways:
One way to resolve this is by simply add spacing (padding) for each character you are printing.
#include<stdio.h>
int main()
{
int j=1,k,l,i;
for(i=4;i>=1;i--){
for(k=i-1;k>=0;k--){
printf(" ");
}
for(l=4;l>=i;l--){
printf(" %2d ",j); //Padding added
^ ^
j++;
}
printf("\n");
}
return 0;
}
Secondly you can change the alignment by using - sign
#include<stdio.h>
int main()
{
int j=1,k,l,i;
for(i=4;i>=1;i--){
for(k=i-1;k>=0;k--){
printf(" ");
}
for(l=4;l>=i;l--){
printf("-%2d",j); //Left justified
^
j++;
}
printf("\n");
}
return 0;
}
Amongst the other good answers, another way is to use printf("%d ", j); instead of printf("%2d", j); - Note the white space ' ' behind the %d format specifier. - This method has the disadvantage that you have a trailing white space after 10 but it accomplishes the (obvious) desired output:
Online example
#include <stdio.h>
int main (void)
{
int j = 1, k, l, i;
for (i = 4; i >= 1; i--){
for (k = i - 1; k >= 0; k--) {
printf(" ");
}
for(l = 4; l >= i; l--) {
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Output:
1
2 3
4 5 6
7 8 9 10
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;
}
I have problem with my code I need to make. I have to take 14 parameters from command line and use them to make lottery numbers, winning numbers and then compare those 2 with each other.
For example using this parameter: ./a.out 2 30 17 8 6 19 24 7 6 1 2 3 5 4
Should make something like this:
Winning numbers: 2 30 17 8 6 19 24
Lottonumbers: 7 6 1 2 3 5 4
2 are the same: 6 2
My code is almost working as intended, but I can't seem to print this right: 2 are the same. It always loops like this: 1 are the same: 6 2 are the same: 2.
Number 2 is the amount of same numbers that are found when 2 arrays are compared. My question is how can I print it so that it won't duplicate the text and with the right amount? My head can't seem to work even if it's so simple :/
#include <stdio.h>
#include <stdlib.h>
int main(int args, char **argv)
{
int i;
int winningNumbers[7];
int lottoNumbers[7];
int j;
int a;
int b;
int winningNumber;
int lottoNumber;
int count = 0;
printf("Winning numbers: ");
for (i=0;i<7; i++) {
winningNumber = atoi(argv[i+1]);
winningNumbers[i] = winningNumber;
printf("%d ", winningNumber);
}
printf("\n");
printf("Lotto numbers:: ");
for (j= 8; j < args; j++) {
lottoNumber = atoi(argv[j]);
lottoNumbers[j-8] = lottoNumber;
printf("%d ", lottoNumber);
}
printf("\n");
for(a = 0; a < 7; a++) {
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
count = count + 1;
printf("%d are the same: %d", count, winningNumbers[b]);
}
}
}
return 0;
}
Searching for matches and displaying the result are two separate tasks. It is simpler and more flexible not to attempt to do them at the same time.
First search for the matches and store them in an array. Then display the content of the array however you want.
int main (int argc, char *argv[])
{
int winningNumbers[7];
int lottoNumbers[7];
int commonNumbers[7];
int count = 0;
// fill winningNumbers
// fill lottoNumbers
// NOTE: the following loop assumes that in both arrays
// no number is repeated.
// You should check that this is indeed the case.
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lottoNumbers[i] == winningNumbers[j]) {
commonNumbers[count] = lottoNumbers[i];
count++;
}
}
}
printf ("%d are the same:", count);
for (int i = 0; i < count; i++) {
printf (" %d", commonNumbers[i]);
}
printf ("\n");
return 0;
}
Many simple programs should follow this structure:
read and check input
transform input to output
print output
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
count = count + 1;
}
}
printf("%d are the same: ", count);
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
printf(" %d", winningNumbers[b]);
}
}
printf("\n");
int finalArray[7];
int i;
for(a = 0; a < 7; a++) {
for(b=0; b < 7; b++) {
if (lottoNumbers[a] == winningNumbers[b]) {
finalArray[count] = lottoNumbers[a];
count = count + 1;
}
}
}
printf("%d are same: ", count);
for(i = 0; i < count; i++)
printf("%d ", finalArray[i]);
How do I print this?
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
I am able to come out the following code but I am unsure on how I can combine the following to produce the result I want.
#include <stdio.h>
int main()
{
int n = 5;
int start = a;
int i;
int j;
for (j = 1; j <= n; j++) /* This for loop produce 1 3 6 10 15 */
{
start = start + j;
printf("%d ", start);
}
printf("\n");
start = 1;
for (i = 0; i < 5; i++) /* This for loop produce 1 2 4 7 11 */
{
start = start + i;
printf("%d ", start);
}
return 0;
}
Please guide me as I am really not good in programming.
If you just want to print the mentioned pattern the fastest way is:
#include <stdio.h>
int main(){
puts("1 3 6 10 15");
puts("2 5 9 14");
puts("4 8 13");
puts("7 12");
puts("11");
return 0;
}
P.S: You are not taking any input from user.
Use nested loops.
#include <stdio.h>
int main()
{
int n = 5;
int row_start = 1, step, start_step = 2; row_step = 1, current;
for (int j = 1; j <= n; j++) /* This for loop produces the rows */
{
current = step = row_start;
step = start_step;
for(int i = j; i <= 5; i++) /* This for loop produces one row */
{
printf("%d ", current);
current += step;
++step;
}
printf("\n");
row_start += row_step;
++row_step;
++start_step;
}
return 0;
}