#include <stdio.h>
void fun(int *, int size, int *, int *new_size);
int main()
{
int size, new_size; // variables declaration
printf("Enter the size : "); // prompting user to enter the size of the array
scanf("%d", &size); // reading size from the user
int arr[size], arr1[size]; // declaring array of size
printf("Enter the elements into the array: "); // prompt the user to enter the elements of array
for (int i = 0; i < size; i++)
{
scanf("%d", arr + i); // reading the elements of array from user
}
fun(arr, size, arr1, &new_size); // calling function
printf("After removing duplicates: "); // prompting user a new array after removing duplicates
for (int i = 0; i < new_size; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
printf("before removing duplicates: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void fun(int *arr, int size, int *arr1, int *new_size) // function definition
{
int count = 1; // declaring variables
arr1[0] = arr[0];
for (int i = 0; i < size; i++)
{
int flag = 0;
for (int j = 0; j < i; j++)
{
if (arr[i] == arr1[j])
{
break;
}
flag = flag + 1;
if (arr1[flag] == 0)
{
*(arr1+flag) = arr[i];
count = count + 1;
}
}
}
*new_size = count;
}
basically what i am doing is, getting the array elements and removing duplicates elements and assigning non-duplicate elements to new array,while without modifying the old array and displaying new array with modification, old array without modification.
But the problem is when i execute this code the output varies for same input values. Why is that i couldn't figure it out.
Here is the output of the program.
Enter the size : 5
Enter the elements into the array: 5
1
2
1
2
After removing duplicates: 5 1 2
before removing duplicates: 5 1 2 1 2
Enter the size : 5
Enter the elements into the array: 5
4
3
2
1
After removing duplicates: 5 4 3 2 1
before removing duplicates: 5 4 3 2 1
Enter the size : 5
Enter the elements into the array: 5
1
2
1
2
After removing duplicates: 5 1 6
before removing duplicates: 5 1 2 1 2
see the output changes
Related
The point of my code is to print all the odd numbers inputted each array using function.
#include<stdio.h>
void printOdd(int arr1[], int arr2[], int s1, int s2) {
int count = 0;
for (int i = 0; i < s1; i++) {
if (arr1[i] % 2 != 0) {
printf("%d", arr1[i]);
count++;
if(count<(s1+s2)/2)
{
printf(", ");
}
}
}
for (int i = 0; i < s2; i++) {
if (arr2[i] % 2 != 0) {
printf("%d", arr2[i]);
if(count<(s1+s2)/2-1)
{
printf(", ");
}
}
}
}
int main(void)
{
int s1, s2;
printf("Enter first array size: ");
scanf("%d", &s1);
int arr1[s1];
printf("Enter second array size: ");
scanf("%d", &s2);
int arr2[s2];
printf("Enter first array values: ");
for(int x = 0; x < s1; x++)
{
scanf("%d", &arr1[x]);
}
printf("Enter second array values: ");
for(int y = 0; y < s2; y++)
{
scanf("%d", &arr2[y]);
}
printOdd(arr1, arr2, s1, s2);
}
These are the expected outputs
Enter first array size: 5
Enter second array size: 5
Enter first array values: 1 2 3 4 5
Enter second array values: 6 7 8 9 10
1, 3, 5, 7, 9
Enter first array size: 3
Enter second array size: 3
Enter first array values: 5 6 10
Enter second array values: 12 41 36
5, 41
Enter first array size: 4
Enter second array size: 3
Enter first array values: 1 2 3 5
Enter second array values: 3 2 1
1, 3, 5, 3, 1
My problem is everytime I input numbers, the result is that there is always a comma at the end, here's my output
Enter first array size: 3
Enter second array size: 3
Enter first array values: 5 6 10
Enter second array values: 12 41 36
5, 41,
https://gyazo.com/9d544951a3572666a3b6b0dc8620d9cc
the link is a picture of expected output and my output
The simplest thing to do is to print the comma before printing your value, and only when count is non-zero. The reason is that you never know if there will be a next odd number, but you do know when there was a previous one. So you should always leave your output with no trailing comma.
void printOdd(int arr1[], int arr2[], int s1, int s2) {
int count = 0;
for (int i = 0; i < s1; i++) {
if (arr1[i] % 2 != 0) {
if (count > 0) printf(", ");
printf("%d", arr1[i]);
count++;
}
}
for (int i = 0; i < s2; i++) {
if (arr2[i] % 2 != 0) {
if (count > 0) printf(", ");
printf("%d", arr2[i]);
count++;
}
}
}
I need to input my own array and give its own elements, from that array i need to print the same one but if theres a number that is prime, it needs to switch it with the next number. Example:
My array: 4 6 3 5 7 11 13
The new array: 4 6 5 3 11 7 13
Here prime numbers are, 3 5 7 and 13, but 13 doesnt have an element to switch itself, so it stays the same.
#include <stdio.h>
#define array 100
int prime(int b
)
{
int i;
for (i = 2; i <= b / 2; i++)
{
if (b % i == 0)
{
return b; // not prime
}
break;
}
return b;
}
int main()
{
int n, i, a[array];
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i++)
{
if (prime(a[i]))
{
int temp;
temp = prime(a[i]);
prime(a[i]) == prime(a[i + 1]);
}
}
printf("\nThe new array is:\n");
printf("%d ", prime(a[i]));
return 0;
}
I haven't learned pointers so is there a way without it or?
there are few things needs to modify
need to change function prime return type to bool. since we are interest to check if array element is Prime. if array element is Prime, return True
int prime(int b)
changed to
bool prime(int b)
also need to extend check if prime() function return true and if array index is not last element then only swap array element to next, else skip
if (prime(a[i]) == 1 && a[i-1] != n)
prost(a[i]) looks typo (I guess). corrected to a[i + 1]
this is not optimized code, it just modified version of your code. if you have concern specific performance, please follow suggestion mentioned by
chux - Reinstate Monica
code:
#include <stdbool.h>
#include <stdio.h>
#define array 100
bool prime(int b)
{
int i;
for (i = 2; i <= b / 2; i++)
{
if (b % i == 0)
{
return false; // not prime
}
break;
}
return true;
}
int main()
{
int n, i, a[array];
int temp;
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i++)
{
if (prime(a[i]) == 1 && a[i] != a[n-1]) /* enter loop only array element is Prime number and it is not last element */
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
a[i++];
}
printf("\nThe new array is:\n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output for above code: check out this link
How many elements does the array have?
7
Put in 7 elements from the array!
4
6
3
5
7
11
13
My array is:
4 6 3 5 7 11 13
The new array is:
4 6 5 3 11 7 13
...Program finished with exit code 0
Press ENTER to exit console.
First of all, you have a for loop that only makes one iteration because of a break keyword, also in main in a for loop with your swapping you need to assign return values from the prime function to variables, and in the same function, you should use singe '=' because you want to assign value but not to compare. Also in your same for loop, you should check if(prime(a[i+1])) so there won't be any segfaults.
I need help on making a program that will count the shortest distance between two places that are arranged in a circular pattern I need to count the distance between first and first, first and second and so on keeping in mind I can go the other way around if the distance is shorter and place the results in a matrix equal to the length of the array that we have inputted at the beginning of the program, so the output should look something like this:
input:
Array length=5
Array elements:2 3 4 1 1
so the output should look like this:
2 3 4 1 1
0 2 5 2 1
2 0 3 4 3
5 3 0 4 5
2 4 4 0 1
1 3 5 1 0
The code that I wrote only gets user input for array length and elements and outputs the first line which is the elements of an array that we have gotten from the user input. So my question is how to get that shortest way for every distance and append those values to the matrix.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int l,n, i, m, x,y,s,j,t;
int **p;
scanf("%d", &l);
ptr = (int*)malloc(l * sizeof(int));
if (ptr == NULL) {
exit(0);
}
else {
for (i = 0; i < l; ++i)
{
scanf("%d", &ptr[i]);
if(ptr[i]<0)
{
exit(0);
}
}
for (i = 0; i < l; ++i) {
printf("%d ", ptr[i]);
}
}
n=l;
m=l;
while(m>0 && n>0){
p= malloc(m*sizeof(int*));
for (i=0;i<m;i++){
p[i]=malloc(n*sizeof(int));
for(j=0;j<n;j++){
x=0;
y=0;
for (t = 0; t < j; t++) {
x = +ptr[t];
}
for (t = n; t >j; t--) {
y = +ptr[t];
}
if (x > y) {
s = y;
} else {
s = x;
}
p[i][j]=s;
}
}
}
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
printf("%d", p[i][j]);
}
putchar('\n');
}
return 0;
}
I am trying to print the following series: 1 2 3 4 5 6 7 8 9 10 ... ... ...The input of my program contains a single integer n, which determines the number of lines to print.
I've tried to code it but got the following output:
12 33 4 54 5 6 7... ... ...
#include<stdio.h>
int main()
{
int n,i,j,t,m;
scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=i,t=1;t<=i;j++,t++)
{
printf("%d ",j);
}
printf("\n");
}
}
To print those numbers, you'll want a counter that starts at 1, increases by 1 every print and is never reset by anything. Adjust your loop like this:
int main()
{
int n, i, j, t = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++, t++)
{
printf("%d ", t);
}
printf("\n");
}
}
Note how t is set to 1, and just gets increased by t++ without resetting like you previously did. Also, you should be printing t, not j.
You should maintain separate counters for the numbers and the number of numbers per line.
int nr = 1, target;
int nrsperline = 1, i;
scanf("%d", &target);
while (nr <= target) {
for (i = 0; i < nrsperline; i++) {
printf("%d ", nr++);
}
printf("\n");
nrsperline++;
}
Hi I have problem with initializing my function for printing an error message if some numbers in an array are same.
#include<stdio.h>
#include<stdlib.h>
void printRepeating(int arr[], int size)
{
int i, j;
for(i = 0; i < size; i++)
for(j = i+1; j < size; j++)
if(arr[i] == arr[j])
printf("Wrong input. Same numbers in array!\n");
}
int main()
{
int arr[200],i;
int res, num;
while((res = scanf("%d", &num)) == 1)
{
arr[i++] = num;
if(num == 0){
break;
}
}
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
printf("\n");
int arr_size = sizeof(arr[i])/sizeof(arr[0]);
printRepeating(arr, arr_size);
return 0;
}
If I scan 1 2 3 1 4 5 0, my function printRepeating wont start nevertheless I have numbers 1 1 that are same in the array, Why ? And another problem is when I type 1 2 3 1 5 0 it only prints 1 2 3 and for example I when I scan 1 2 3 4 5 6 7 8 9 0 it prints all numbers except for 0.
There are multiple issues in your code. First, initialize i to be 0, and declare a new variable j alongside i,
int arr[200], i = 0, j;
The size of your array would simply be i, which you increment every time you insert an element in the array, and change this,
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
to this
for(j = 0; j < i; j++)
printf("%d ", arr[j]);
since the size of your array is stored in variable i. Also, the way you are calculating the size of your array is wrong which returns 1 everytime since the numerator and denominator are the same. Generally, it is sizeof(array)/sizeof(array[0]), and in this case, it too, would return 200, since the size of your declared array is 200, but since you increment your i every time at insertion, simply set your arr_size to be i.
int arr_size = i;
This line
int arr_size = sizeof(arr[i])/sizeof(arr[0]);
does not do what you are expecting. You are only dividing the size of two elements of the array, which are always the same size, thus always giving 1 as result. If you want to give the number of elements to the function, give it a variable that you used to count each number as you read them.
Also this:
for(i = 0; i < arr[i]; i++)
printf("%d ", arr[i]);
Is only printing numbers that are not larger than their indices. Again, your program is missing a variable to count the number of input values.