I'm trying to create a program that outputs the second positive value in an array of integers. If there is no second positive number, it will output "not positive enough." However, my function doesn't work for some reason. Would someone be able to point out why? Thanks :)
#include <stdio.h>
#define NOT_POSITIVE_ENOUGH 0
int array_second_positive(int size, int array[size]) {
int second_array[size];
int i = 0;
int j = 0;
while (i < size) {
if (array[i] > 0) {
scanf("%d", &second_array[j]);
j++;
}
i++;
}
if (j < 2) {
return 0;
}
else {
return second_array[1];
}
}
#define MAX_SIZE 100
int main(void) {
int size1 = 7;
int array1[MAX_SIZE] = {3, -14, 15, 9, 2, 6, 5};
int result1 = array_second_positive(size1, array1);
if (result1 == NOT_POSITIVE_ENOUGH) {
printf("array1 wasn't positive enough!\n");
} else {
printf("The second positive value from array1 is: %d\n", result1);
}
return 0;
}
First of all, scanf("%d", &second_array[j]); probably does not do what you think it does. Refer to (for example): scanf, and consider using:
second_array[j] = array[i]; instead.
Though a simpler and more concise approach would be:
int array_second_positive(int size, int *arr) {
int found = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > 0) {
if (found) return arr[i];
found = 1;
}
}
return 0;
}
The error was simple , please change :-
scanf("%d", &second_array[j]);
To :
second_array[j] = array[i];
Be careful when you code !
Related
I am supposed to write a program that prints the minimum value from vector.This is what i tried. It only prints 0. I tried to change the sign both ways but it doesnt work.
#include <stdio.h>
int read(int v[], int size)
{
int i = 0;
do
{
scanf("%i", &v[i]);
i++;
} while (v[i-1] != 0 && i < size);
int n = i;
return n;
}
int minim(int v[], int n)
{
int m;
m = v[0];
int i;
for (i = 1; i <= n-1; i++)
{
if (v[i] < m)
{
m = v[i];
}
}
return m;
}
int main()
{
int arr[100];
int n = read(arr, 100);
int min = minim(arr, n);
printf("\nMinimum vrom vector is %i\n", min);
return 0;
}
Since your scanf loop (I'd recommend staying away from function names like read, which are part of the C standard, even if you didn't include unistd.h) ends when 0 is entered, you need to include a check at the end to decrement the size of the array if 0 is the last entry. Basically, replace everything after your do-while loop with this:
if (v[i - 1]) {
return i;
}
return --i;
This will return i if all 100 elements are non-zero, otherwise it will decrement to remove the 0 from your array before returning. No need to declare int n=i just to instantly return n.
Edit: I saw your comment that it worked properly for finding the maximum. This is because you almost certainly entered a number into the array that's greater than 0, so adding 0 at the end would not affect the maximum number. Try finding the max again, but only enter negative numbers. The result will be 0.
read() uses a 0 entry to terminate reading more input. Yet that 0 is included in the array and counts toward the array length as part of the return value.
Instead, only increment the array count when input was numeric and non-zero.
int read(int v[], int size) {
int i = 0;
while (i < size) {
// Also test if valid numeric input was read.
if (scanf("%i", &v[i]) != 1) {
break;
}
// Stop if a 0 was read
if (v[i] == 0) {
break;
}
// Now increment
i++;
}
return i;
}
Could use shorter code, yet it is less readable. Best to code for clarity.
int read(int v[], int size) {
int i = 0;
while (i < size && scanf("%i", &v[i]) == 1 && v[i]) {
i++;
}
return i;
}
The test condition
while (v[i-1] != 0
checks whether the last element read was 0, after it was successfully processed and converted by scanf (but you never checked the return value). The 0 is then included in the array, and will always be the minimum unless you enter a non-negative number.
Here's the working code:
#include <stdio.h>
#include <stdlib.h>
static size_t read (int v[], size_t size)
{
size_t i = 0;
do
{
/* Check if scanf was successful */
if (scanf("%i",&v[i]) != 1) {
fprintf(stderr, "Error: Invalid input.\n");
return EXIT_FAILURE;
}
} while (v[i] != 0 && ++i < size);
return i;
}
static int minim (int v[],size_t n)
{
int min;
min = v[0];
for(size_t i = 1; i < n; i++) {
if (v[i] < min) {
min = v[i];
}
}
return min;
}
int main(void)
{
int arr[100];
size_t n = read(arr,100);
int min = minim(arr,n);
printf("\nMinimum vrom vector is %i\n", min);
return EXIT_SUCCESS;
}
I made some minor changes to it. Though I'm not satisfied with the design.
Sample I/O:
20
8
18
60
39
56
0
Minimum vrom vector is 8
I have no idea what's wrong with the function goes by the name "int Count_largest_even". It's supposed to take the largest digit found in the given array (by the function "int find") and find how many times the digit appears in the array.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Count_largest_even(int size, int *array, int large);
void ArrayPrint(int a[], int size);
int find(int array[], int size);
int arr1[16] = { 2, 22, 1, 3, 24, 94, 93, 12, 12, 66666, 21, 24, 8888, 21, 2, 33 };
int main() {
int mount;
int even;
ArrayPrint(arr1,16);
even = find(arr1, 16);
mount = Count_largest_even(16, arr1, even);
printf("\n The biggest even digit is : %d\n %d", even,mount);
system("pause");
return 0;
}
int find(int array[], int size){
int i = 0, digit, edigit = 0;
for (i = 0; i<size; i++){
while (array[i]!=0)
{
digit = abs(array[i] % 10);
if (digit > edigit)//checking condition for large
{
if (digit % 2 == 0)
{
edigit = digit;
}
}
array[i] = array[i] / 10;
}
}
return edigit;
}
void ArrayPrint(int a[], int size)
{
int i;
for (i = 0; i<size; i++){
printf("%d\n", a[i]);
}
}
int Count_largest_even(int size, int *array, int large)
{
int i;
int count = 0, digit;
for (i = 0; i < size; i++){
while ((array[i]!=0))
{
digit = abs(array[i] % 10);
if (digit == large)
{
count++;
}
array[i] = array[i] / 10;
}
}
return count;
}
As Ian Abbott said, you should not modify the array inside your loop.
But you can also do this in a single pass - some pseudo code:
int count = 0;
int largest_digit = 0;
for each digit:
if(digit > largest_digit) {
largest_digit = digit;
count = 1;
}
else if(digit == largest_digit)
count++;
Edit: I need to maintain the order in which the elements are present in the original array, so sorting won't work.
I have a 1-D array containing some elements and I am using printf() in C but I only want to print an element if and only if it has not already been printed before.
I am thinking of using nested loops to compare if the element I am about to print from the current position in the array was already present in a lower index of the array but it's not working. What am I missing? Or is my whole approach wrong?
So far, I have tried this, which is not working:
int arr[20];
After this I take user input for no. of elements in p and of course, p<20. Then, user enters the elements one by one. I use scanf() for this.
for(i=1;i<=p;i++)
{
for(j=i+1;j<=p;j++)
{
if(arr[i]!=arr[j])
{
printf("%d",arr[j]);
}
}
}
You need to check all previous items before you know if the item has already occurred.
#include <stdio.h>
#include <stdlib.h>
int compareFunc(const void *op1, const void *op2 )
{
int *a, *b;
a = (int*)op1;
b = (int*)op2;
return *a - *b;
}
void printUnique(int *array, int numElems)
{
int curPrintIndex, curCompareIndex;
char alreadySeen;
for (curPrintIndex=0; curPrintIndex<numElems; curPrintIndex++)
{
alreadySeen = 0;
for (curCompareIndex=0; curCompareIndex<curPrintIndex; curCompareIndex++)
{
if (array[curCompareIndex] == array[curPrintIndex])
{
alreadySeen = 1;
break;
}
}
if (alreadySeen == 0)
printf("%d\n", array[curPrintIndex]);
}
}
int main()
{
const int numItems = 100;
int *array, i, lastVal;
array = calloc(numItems, sizeof(int) );
for (i=0; i<numItems; i++)
array[i] = rand()%numItems;
printUnique(array, numItems);
free(array);
return 0;
/*
qsort(array, numItems, sizeof(int), compareFunc);
printf("%d\n", array[0]);
lastVal = array[0];
for (i=1; i<numItems; i++)
{
if (array[i] != lastVal)
{
lastVal = array[i];
printf("%d\n", array[i]);
}
}
*/
}
#include <stdio.h>
int main(void){
int arr[20] = { 4,8,4,2,4,8,1,3,2,7 };//the elements are positive integers.
int i, j, p = 10;
for(i=0;i<p-1;i++){
if(arr[i] < 0)
continue;
for(j=i+1;j<p;j++){
if(arr[j] > 0 && arr[i]==arr[j])
arr[j] *= -1;
}
}
for(i=0; i < p; ++i)
if(arr[i] > 0)
printf("%d ", arr[i]);
puts("");
return 0;
}
I'm stuck on Euler#4 which is to calculate the highest palindrome number by product of two 3-digit numbers. The answer I'm getting is always 0. Evidently help is required.
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,h=0,m=0,p=0;
clrscr();
for(i=100;i<1000;i++)
{
for(j=100;j<1000;j++)
{
p=i*j;
h=p/100000;
m=p%10;
if(h==m)
{
h=(p/10000)%10;
m=(p/10)%10;
if(h==m)
{
h=(p/1000)%10;
m=(p%1000)/100;
if(h==p)
{
printf("%d\n",p);
}
}
}
}
}
return 0;
}
Its wrong to do this, but still dont make it a habit. I think one can solve first 50 question with ease.
#include <stdio.h>
static int is_palindromic(unsigned int n);
int main(void)
{
unsigned int i, j, max = 0;
for (i = 100; i <= 999; i++) {
for (j = 100; j <= 999; j++) {
unsigned int p = i*j;
if (is_palindromic(p) && p > max) {
max = p;
}
}
}
printf("%u\n", max);
return 0;
}
int is_palindromic(unsigned int n)
{
unsigned int reversed = 0, t = n;
while (t) {
reversed = 10*reversed + (t % 10);
t /= 10;
}
return reversed == n;
}
I am trying to find the max number in an array. I have created a function and I am using the following code:
int maxValue( int myArray [], int size)
{
int i, maxValue;
maxValue=myArray[0];
//find the largest no
for (i=0;i)
{
if (myArray[i]>maxValue)
maxValue=myArray[i];
}
return maxValue;
}
However I get a syntax error before ) token. What am I doing wrong and am I even doing this right? Any help would be greatly appreciated.
You must pass a valid array with at least one member to this function:
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int
maxValue(int myArray[], size_t size) {
/* enforce the contract */
assert(myArray && size);
size_t i;
int maxValue = myArray[0];
for (i = 1; i < size; ++i) {
if ( myArray[i] > maxValue ) {
maxValue = myArray[i];
}
}
return maxValue;
}
int
main(void) {
int i;
int x[] = {1, 2, 3, 4, 5};
int *y = malloc(10 * sizeof(*y));
srand(time(NULL));
for (i = 0; i < 10; ++i) {
y[i] = rand();
}
printf("Max of x is %d\n", maxValue(x, sizeof(x)/sizeof(x[0])));
printf("Max of y is %d\n", maxValue(y, 10));
return 0;
}
By definition, the size of an array cannot be negative. The appropriate variable for array sizes in C is size_t, use it.
Your for loop can start with the second element of the array, because you have already initialized maxValue with the first element.
A for loop has three parts:
for (initializer; should-continue; next-step)
A for loop is equivalent to:
initializer;
while (should-continue)
{
/* body of the for */
next-step;
}
So the correct code is:
for (i = 0; i < size; ++i)
the paren after the for seems to be missing some contents.
normally it should be something like
for (i=0; i<size; i++)
include:
void main()
{
int a[50], size, v, bigv;
printf("\nEnter %d elements in to the array: ");
for (v=0; v<10; v++)
scanf("%d", &a[v]);
bigv = a[0];
for (v=1; v<10; v++)
{
if(bigv < a[v])
bigv = a[v];
}
printf("\nBiggest: %d", bigv);
getch();
}