I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9
Related
I've been trying to solve an assignment where you:
Enter the size N of an array of ints
Enter the max value M of the ints that you want to populate the array with
Enter N values that are of a value between 1 and M, into a second array.
compare these two and print out the missing numbers...
Like this:
Size of array? 10 // => N = 10
Max value in array? 8 // => M = 8
Please enter 10 values between 1 and 8:
4 1 3 1 7 3 4 4 6 1
Missing values: 2 5 8
for some reason, my for loop just prints out all the numbers between 1 and M instead, no matter what I try... What am I missing??
code:
#include <stdio.h>
int main(void)
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue];
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < mValue; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
for (int j = 0; j < mValue; j++)
{
if(table[i] != values[j] && table[i] != 0)
{
printf("%d ", table[i]);
break;
}
}
}
}
#include <stdio.h>
int main()
{
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[aSize]; // not 'mSize' because it is just max value not size of array
for (int i = 0; i < aSize; i++)
{
table[i] = i+1;
if ((i+1) > mValue)
{
table[i] = 0;
}
}
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &values[i]);
}
for(int i = 0; i < aSize; i++)
{
int flag=0;
for (int j = 0; j < aSize; j++)
{
if(table[i] == 0 || table[i] == values[j]) // numbers in common or zero
{
flag=1;
break;
}
}
if(flag == 0) printf("%d",table[i]); // missing numbers
}
}
In this code, I'm creating an extra array to store the occurance of the variable, then printing it. 1 means present 0 means not present.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int table[aSize];
int values[mValue+1];
for (int i = 0; i <= mValue; i++)
{
values[i]=0;
}
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &table[i]);
if(table[i]>mValue || table[i]<1){
printf("Enter numbers in given range");
return 0;
}
}
for(int i = 0; i < aSize; i++)
{
values[table[i]]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
return 0;
}
This is better version of the above logic.
#include <stdio.h>
int main(void) {
int aSize, mValue;
printf("Size of array? ");
scanf(" %d", &aSize);
printf("Max value in array: ");
scanf(" %d", &mValue);
int temp;
int values[mValue+1];
values[0]=1;
printf("Please enter %d values between 1 and %d:\n", aSize, mValue);
for (int i = 0; i < aSize; i++)
{
scanf(" %d", &temp);
if(temp>mValue || temp<1){
printf("Enter numbers in given range");
return 0;
}
values[temp]=1;
}
printf("Missing values:\n");
for(int i = 1; i <= mValue; i++)
{
if(!values[i])
printf("%d\n",i);
}
return 0;
}
I want to create a program that can read scores from user and display the scores output in the form of stars. But it seems that my code keep getting an infinity loop. Can anyone tell me what is wrong with my looping.
#include <stdio.h>
int main() {
int i, k, j;
int score[5];
for(i = 1; i < 6; i++) {
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 1; k < 6; k++) {
printf("%d. ", k);
for (j = 1; j <= score[k]; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Array indices are 0 based in C. So for an array with 5 elements the indices are from 0-4 inclusive. You are using indices 1-5 which results in buffer overflow. Below is the corrected program with the for loops fixed up to use the right indices:
#include <stdio.h>
#define NUM_SCORES 5
int main()
{
int i, k, j;
int score[NUM_SCORES];
for(i = 0; i < NUM_SCORES ; i++)
{
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 0; k < NUM_SCORES ; k++){
printf("%d. ", k);
for(j = 1; j <= score[k]; j++){
printf("*");
}
printf("\n");
}
return 0;
}
You should also add a check to ensure scanf is able to successfully read each input.
How can I copy the elements from a matrix,entered by user,to an array? I tried this, but it didn't work:
#include<stdio.h>
int main(){
int m[20][20],a[400],c=0;//max dimensions;
scanf("%d %d",&M,&N);//dimensions of matrix;
for(i=0;i<M;i++{
for(j=0;j<N;j++{
scanf("%d", &m[i][j]);
for(c=0;c<M*N;c++)
a[c]=m[i][j];
}}}
Don't know why you want to store both the matrix format and the array format, but anyway here is a code that should do the trick with also the data output to show the result:
#include <stdio.h>
#include <stdlib.h>
#define R 20 //max rows
#define C 20 //max columns
int main() {
int m[R][C]; //matrix
int a[R*C]; //array
int r, c; //user matrix size
int i, j; //iterators
printf("insert row size: ");
scanf("%d", &r);
printf("insert column size: ");
scanf("%d", &c);
if(r > R || c > C) {
printf("Invalid sizes");
return -1;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("insert value for row %d column %d: ", i + 1, j + 1);
scanf("%d", &m[i][j]);
a[(c * i) + j] = m[i][j];
}
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
for(i = 0; i < r * c; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Note that I also added some checks for the user data, avoiding to generate a matrix bigger that the maximum size. Also you don't need separate loops but it can be done all together.
Also please post a code that is compilable and can be run, as explained here: https://stackoverflow.com/help/how-to-ask
Hello I'm trying to write a program about subsets and my problem is that whenever I try to run the program, it's automatically saying that "Y is a not subset of X" even though it is a subset. Can someone help me with this? This is my code:
#include<stdio.h>
int main()
{
int i, j, size1, size2, flag;
printf("S U B S E T S\n\n");
printf("Enter number of digits for Array X: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter number of digits for Array Y: ");
scanf("%d", &size2);
int array2[size2];
printf("\n");
printf("Enter %d digits for Array X: ", size1);
for(i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("\n");
printf("Enter %d digits for Array Y: ", size2);
for(i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
printf("\n\n");
for(i = 0; i < size2; i++)
for(j = 0; j < size1; j++)
if(array2[i] == array1[j])
flag++;
if(flag == size1)
{
printf("Array X is a subset of Array Y");
}
else
{
printf("Array X is not a subset of Array Y");
}
return 0;
}
It is working with very minor changes. Just loop from 0 and use < instead of <=.
#include<stdio.h>
int isSubset(int arr1[], int arr2[], int m, int n) {
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}
return 1;
}
int main() {
int arr1[20];
int arr2[20];
int m, n;
printf("How many elements of X you want to store?\n");
scanf("%d", &m);
printf("How many elements of Y you want to store?\n");
scanf("%d", &n);
for (int i = 0; i < m; i++) {
printf("\nEnter %d X values to be stored:\n", m);
scanf("%d", &arr1[i]);
}
for (int j = 0; j < n; j++) {
printf("\nEnter %d Y values to be stored:\n", n);
scanf("%d", &arr2[j]);
}
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("Y is a subset of X \n");
else
printf("Y is not a subset of X\n");
getchar();
return 0;
}
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}