Removing repetitions of same numbers in an array - c

Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}

Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++

Related

My code generates a random integer output and I do not know why

I am having difficulties with the output of a program for an assignment and I cannot figure out what is causing my problem. The code is as follows:
#include <stdio.h>
int find_minimum(int *a, int n)
{
int *last=(a + n);
int minimum = *a;
while(a!=last){
if(*a < minimum)
minimum=*a;
a++;
}
return minimum;
}
int main()
{
int N;
printf("Enter number of parts (N): ");
scanf("%d",&N);
int K;
printf("Enter number of part types (K): ");
scanf("%d",&K);
int a[K];
printf("Enter Part list:\n");
for(int i=0;i<N;i++){
int part;
scanf("%d",&part);
a[part-1]+=1;
}
printf("The factory can build %d computer(s)",find_minimum(a,K));
return 0;
}
This program is supposed to take input for the amount of parts and how many types of parts there are, and then calculate how many possible combinations of computers can be made out of the parts entered. The example input I was given looks like:
Enter the number of parts (N): 10
Enter the number of types of parts (K): 2
Enter part list:
1 1 1 1 1 2 2 2 2 2
The factory can build 5 computer(s)
Upon entering these numbers into the program, I get a randomly generated integer, which is not the intended result. Any help would be appreciated!
You declared your array but you did not initialize it. It means that every slot of your array does not have a 0 inside but can have a random value in it.
So doing a[part-1] += 1 add 1 to a random value (note that if part is 0 or > to k you are out of bound). You need to initialize every "slot" to 0.
try to compile and execute this to understand:
#include <stdio.h>
int main()
{
int arr[5];
for (int i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
}

Change For Loop into While Loop

I'm new to programming and I need to switch this code into a while Loop.
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input number of terms : ");
scanf("%d",&n);
printf("\nThe odd numbers are :");
for(i=1;i<=n;i++)
{
printf("%d ",2*i-1);
sum+=2*i-1;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);
}
The question is Write a program in C to display the n terms of odd natural number and their sum
The loop continuation condition stands alone while initialization and incrementation are done separately, before and within the loop, respectively.
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input number of terms : ");
scanf("%d",&n);
printf("\nThe odd numbers are :");
i = sum;
while(i<=n)
{
printf("%d ",2*i-1);
sum+=2*i-1;
i++;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d.
\n",n,sum);
}
You can use a 'do while' loop if you know the first iteration will always take place; the loop continuation condition is evaluated after execution.
do {
...
} while(i <= n);
A for loop consists of 4 parts:
for ( initial_expression; loop_condition; update_expression )
{
//body
}
initial_expression gets executed once, when entering the loop.
loop_condition is checked when entering the loop, and after each iteration
update_expression is executed each iteration. ( NOT at the first entry to loop )
body is executed each iteration.
Whereas a while loop consists of 2 parts:
while ( loop_condition )
{
//body
}
In order to make your while loop behave as your for loop, you need to add other two parts yourself.
initial_statement;
while ( loop_condition )
{
//body
update_expression;
}
This is identical to the previous for loop. I recommend you to check flowcharts for while and for loops to get better understanding of work flow.
while (i++<=n) {
/* whatever */
}
will do the job

Fibonacci sequence while loop

I have to write code that displays the Fibonacci sequence to the user desired number of terms and must also use a while loop. I'm not sure why this code isn't working.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int a=0;
int b=0;
a=2;
while(a<max) {
if((a==0||a==1))
{
printf("%i\n", &a);
++a;
}
else if(a>1)
{
a=(a-1)+(a-2);
printf("%i\n", &a);
++a;
}
}
return 0;
}
You can try this.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int n=0;
int a=0;
int b=1;
int next;
while(n<max) {
if ( n <= 1 )
{
next = n;
n++;
}
else
{
next = a + b;
a = b;
b = next;
n++;
}
printf("%d\n", next);
}
return 0;
}
issues with your code:
following declaration & initialisation, you set a=2 => it won't take the true branch of the if statement -- '0' will not be printed in your result.
a=(a-1)+(a-2); i.e a = 1
then you are doing ++a; => a == 2. thus it again else statement with same a==2.
hence it will print the same value and loop executes infinitely.
In the very beginning of your program (before the while loop) a is 2 (see the line a=2).
And in the while loop you do following:
a=(a-1)+(a-2); // a = 2-1+2-2 i.e. a = 1
and right after it
++a; // a == 2
So, after it a==2 again. This loop never ends.
But it is technical problem. More important is that you are trying to calculate not Fibonacci Sequence. In the Fibonacci Sequence each subsequent number is the sum of the previous two. But in your code there is adding of not two previous numbers of Fibonacci Sequence, but previous two Natural numbers.
You have variable b, because someone told you to add it. And it was right! Just remember previous found element of the Fibonacci Sequence in b. When you know previous one element and current one, it is possible to calculate next one.

Scan and sum using array in C

I'm trying to write a simple program that'll prompt the user to enter N numbers, store them in an array, then just sum them all up
I understand I can just do this with a recursion but I'm trying to learn how array works
Example:
1 (hit enter)
2 (hit enter)
...
10 (hit enter)
Expected output: 55
#include <stdio.h>
int main (void){
int n;
int a[n];
int counter;
printf("How many numbers do you want to enter? \n");
scanf("%d", &n);
printf("OK! now enter your number: \n");
for (int i = 0; i <= n; i++){
scanf("%d", &a[i]);
counter =+ a[i];
}
printf("The answer is: %d\n", counter);
return 0;
}
Right now there's no error message, no output, just the standard windows error message
"scanner.exe has stopped working..."
I'm using Win8 and GCC compiler
First of all, you can't create an static array without first knowing its size. You first need to ask the user for the "n" variable and then declare your array.
You also need to explicitly initialize your counter variable to be zero before you start counting. In C, variables don't default to 0 when you declare them.
The operator "=+" doesn't exist AKAIK, change it to "+=".
Last but not least, the limit in your loops is a little off, you're asking for 11 values ;)
(I edited this post, I was wrong about only asking for 9 values. I tend to confuse that sort of stuff)
#include <stdio.h>
int main (void){
int n;
int counter = 0;
printf("How many numbers do you want to enter? \n");
scanf("%d", &n);
int a[n];
printf("OK! now enter your number: \n");
for (int i = 0; i < n; i++){
scanf("%d", &a[i]);
counter += a[i];
}
printf("The answer is: %d\n", counter);
return 0;
}
You are using variable length arrays. At run time the value of n must be known. Place the declaration
int a[n];
after taking input for n, i.e, after scanf("%d", &n); and initialize counter to zero before using it otherwise you will get garbage value (because of undefined behavior).
Also change the for loop condition from i <= n to i < n.
After this line:
int n;
What do you think the value of n is?
Now go to the next line:
int a[n];
How big is this array?
Can you access it properly?

Problems with arrays

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);

Resources