C programming - Incorrect output of matches - c

The task is to enter the dimension and data into a two dimensional array and determine the number of non repeating numbers. I tried many things so i asked you to give me your instructions or hints(wanna instructions) how to correct my code.
#include <stdio.h>
#include <stdlib.h>
int protect(int max_size_1 ,int max_size_2 , int current_1,int current_2,const int arr[max_size_1][max_size_2])
{
for(int f1=0;f1<max_size_1;f1++)
{
for(int f2=0;f2<max_size_2;f2++)
{
printf("%d---%d\n",arr[current_1][current_2],arr[f1][f2]);
if((f1!=current_1 && f2!=current_2) && arr[current_1][current_2]==arr[f1][f2])
{
return 0;
}
}
}
return 1;
}
int main()
{
int i, j, f1, f2, count=1;
printf("[i][j] = ");
scanf("%d", &i);
int arr[i][i];
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
printf("a[%d][%d] = ", f1, f2);
scanf("%d", &arr[f1][f2]);
}
}
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
if (protect(i,j,f1,f2,arr)==1){
count++;}
}
}
printf("\n\n%d", count/2);
return 0;
}

I believe, you are trying to store data in a 2 dimensional array.
and in next step for every value in 2 dim array, trying to find whether there is any dup entry present in array. No sure of your requirement to have data stored in 2 dim array, also there are other optimized way of removing duplicates from user inputs.
we have n user inputs
want to print the number of entries which do not have a dup in data entered by user.
assume,
if user input is
1, 1
1, 1
answer should be 0.
for input
1, 2
3, 4
answer should be 4.
could see three issues in the code
you have two variables "i" and "j", only one of this var is getting a value assigned , j is always 0. this makes the "protect" function to return a 1 as output for very call.
count is started from 1, should be 0
result is getting devided by 2, not sure why this is required
#include <stdio.h>
#include <stdlib.h>
int protect(int max_size_1 ,int max_size_2 , int current_1,int current_2,const int arr[max_size_1][max_size_2])
{
for(int f1=0;f1<max_size_1;f1++)
{
for(int f2=0;f2<max_size_2;f2++)
{
printf("%d---%d\n",arr[current_1][current_2],arr[f1][f2]);
if((f1!=current_1 && f2!=current_2) && arr[current_1][current_2]==arr[f1][f2])
{
return 0;
}
}
}
return 1;
}
int main()
{
int i, j, f1, f2, count=0;
printf("[i][j] = ");
scanf("%d", &i);
int arr[i][i];
j = i;
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
printf("a[%d][%d] = ", f1, f2);
scanf("%d", &arr[f1][f2]);
}
}
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
if (protect(i,j,f1,f2,arr)==1){
count++;
}
}
}
printf("\n\n%d\n", count);
return 0;
}

Related

Program to find non-repeating values in a two-dimensional array, incorrect answer

The task is to enter the dimension and data into a two dimensional array and determine the number of non repeating numbers. I tried many things so i asked you to give me your instructions or hints(wanna instructions) how to correct my code.
#include <stdio.h>
#include <stdlib.h>
int protect(int max_size_1 ,int max_size_2 , int current_1,int current_2,const int arr[max_size_1][max_size_2])
{
for(int f1=0;f1<max_size_1;f1++)
{
for(int f2=0;f2<max_size_2;f2++)
{
printf("%d---%d\n",arr[current_1][current_2],arr[f1][f2]);
if((f1!=current_1 && f2!=current_2) && arr[current_1][current_2]==arr[f1][f2])
{
return 0;
}
}
}
return 1;
//0-if there are repetitions 1- if no
}
int main()
{
int i, j, f1, f2, count=1;
//enter the elements of array
printf("[i][j] = ");
scanf("%d", &i);
int arr[i][i];
for (f1 = 0; f1<i; f1++)
//loop through rows
{
for (f2 = 0; f2<i; f2++)
//cycle through columns
{
printf("a[%d][%d] = ", f1, f2);
scanf("%d", &arr[f1][f2]);
}
}
for (f1 = 0; f1<i; f1++)
//loop through rows
{
for (f2 = 0; f2<i; f2++)
//cycle through columns
{
if (protect(i,j,f1,f2,arr)==1){
count++;}
}
}
printf("\n\n%d", count/2);
return 0;
}

Finding the second positive value in an array

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 !

printing unsigned array and exiting the loop

Summary:
I want to be able to write a function that can let me store 10 values. I should be able to exit the loop with 0 without storing 0 to the array. I should be able to re-enter the array and keep storing until i get 10 values.
Questions:
I started to write something simple but when I store like 5 values it will print out the 5 values and then some random numbers. Why is that?
And how can I exit the loop without the array storing the 0?
I'm quite new to this stuff so I hope I've followed the rules correctly here.
Code:
#include <stdio.h>
int main(void)
{
int arrayTable[9] = {0};
int i;
for (i=0; i<10; i++)
{
printf("Enter Measurement #%i (or 0): ", i+1);
scanf("%d", &arrayTable[i]);
if (arrayTable[i] == 0)
{
break;
}
}
for (int i=0; i<10; i++)
{
printf("%d\n", arrayTable[i]);
}
return 0;
}
#include <stdio.h>
#define ArraySize 10
int main(void){
unsigned v, arrayTable[ArraySize] = {0};
int n = 0;//number of elements
while(n < ArraySize){
printf("Enter Measurement #%i (or 0): ", n + 1);
if(1 != scanf("%u", &v) || v == 0){//use other variable
break;
}
arrayTable[n++] = v;
}
for (int i = 0; i < n; ++i) {
printf("%u\n", arrayTable[i]);
}
return 0;
}
as long as you want discard 0 from array then use a temporary variable, input it, check whether it is a non-zero and if so store it to the element of array, if it is a zero exit the loop:
#include <stdio.h>
int main(void)
{
int arrayTable[10] = {0};
int iValue = 0;
int i = 0;
while(i < 10)
{
printf("Enter Measurement #%i (or 0): ", i+1);
scanf("%d", &iValue); // input iValue
if (!iValue) // if iValue is zero then exit loop without affecting array with this value
break;
else
{
arrayTable[i] = iValue; // if the value is non-zero store it in array and continue
i++;
}
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", arrayTable[i]);
}
return 0;
}
You probbaly want this:
...
int arrayTable[10] = {0}; // <<< [10] instead of [9]
...
for (i=0; i<10; i++)
{
if (arrayTable[i] == 0) // <<< add this
break; // <<<
printf("%d\n", arrayTable[i]);
}
...

Find 2nd largest element from stdin without using an array

#include<stdio.h>
int main() {
int i, m1, m2, n, num;
puts("\n");
scanf("%d",&n);
for(i = 0; i < n; i++) {
scanf("%d", &num);
if(i == 0) {
num = m1 = m2;
}
if(num > m1) {
m2 = m1;
m1 = num;
} else if(num > m2) {
m2 = num;
}
}
return 0;
}
my stdin: -950 -588 -169 -187 -445 400 -1
I have to get stdout: -169 but its showing stdout: \n
Note: I want to solve this problem without arrays.
the statement:
num = m1 = m2;
is wrong and it does not cause the three variables to have the same value. You need to assign m1 and m2 to num. You are overwriting the variable that you previously had read. Change it to:
m1 = num;
m2 = num;
Then, print out the m2.
EDIT:
As others found out, the -1 states for end of your input. Adding simple if statement solves the problem and for your input -169 is the second largest element.
Full code:
#include<stdio.h>
int main(){
int i, m1, m2, n, num;
puts("\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&num);
if(i==0)
{
m1 = num;
m2 = num;
}
else if(num == -1) /* if -1 was read, then terminate the loop. */
{
break;
}
else if(num>m1)
{
m2 = m1;
m1 = num;
}
else if(num>m2)
{
m2=num;
}
}
printf("%d\n",m2);
return 0;
}
For input:
7
-950 -588 -169 -187 -445 400 -1
and current code output is -169.
Another EDIT:
Ok, your code is wrong because of the scanf for number of elements. In future It would be helpful if you were more clear about your problems. I hope that following code will work for you.
#include <stdio.h>
int main(void)
{
int curr, second, first;
scanf("%d", &curr);
second = curr;
first = curr;
while (1) {
scanf("%d", &curr);
if (curr == -1) {
break;
}
if (curr > first) {
second = first;
first = curr;
}
else if (curr > second) {
second = curr;
}
}
printf("%d\n",second);
return 0;
}
/* Program to find the second largest number without using array */
main()
{
int num,large=0,slarge=0,i=0;
clrscr();
printf("Enter the number:");
while(i<10)
{
scanf("%d",&num);
if(i==0)
{
large=num;
}
else if(num>large)
{
slarge=large;
large=num;
}
else if(num>slarge)
{
slarge=num;
}
i++;
}
printf("Large number:%d",large);
printf("\nSecond large=%d",slarge);
getch();
return 0;
}
The problem with your code is you are using m2 uninitialized. To correct the problem, set m2 to some reasonable negative number (like the smallest integer allowed). Here we are just using a negative number for example:
m2 = -1000000;
outout
argument [0]: -950
argument [1]: -588
argument [2]: -169
argument [3]: -445
argument [4]: 400
argument [5]: -1
m1: 400
m2: -1
Your code does what you intend. -1 is the second largest number (400 is the largest). If you want -169, then you want the 3rd largest. Remember:
ALWAYS INITIALIZE YOUR VARIABLES
here's a working and simpler version :
#include <stdio.h>
#include <limits.h>
int main(int argc , char** argv)
{
int m1 , m2 , rc = 1;
m1 = m2 = INT_MIN ;
while(rc)
{
scanf("%d" , &rc);
if(rc > m1)
m1 = rc;
else if(rc < m1 && rc > m2)
m2 = rc;
}
printf("%d\n" , m2);
}
/*Second largest elements in a given array*/
#include <stdio.h>
int SecondMax(int a[], int n) // n= array size
{
int max1, max2; //assume max1 as largest and max2 is second largest
max1= max2= a[0]; //Initialize first element of array
for(int i=0; i<n; i++)
{
if(a[i] > max1) //check each elements of array with max1
{
max2= max1;
max1= a[i];
}
else if(a[i] > max2)
max2= a[i];
}
return max2;
}
int main()
{
int a[10]={2, 54, 8, 9 ,12, 6, 3, 7, 32, -5};
printf("\nmax2= %d", SecondMax(a, 10)); //print return value
return 0;
}

3x3 array = 10 numbers

i have this code
#include <math.h>
#include <stdio.h>
const int n = 3;
const int s = 3;
int getm(int mat[n][s]);
int printm(int mat[n][s]);
int main()
{
int m[n][s];
getm(m);
printm(m);
return 0;
}
int getm(int mat[n][s])
{
for(int x = 0;x < n;x++)
{
for (int y = 0;y<s;y++)
{
scanf("%i ", &mat[x][y]);
}
}
return 0;
}
int printm(int mat[n][s])
{
for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
if(y==(s-1))
{
printf("\n");
}
}
}
}
which shoud ask for 9 numbers to make a 3x3 matrix array, but it actually asks for 10 numbers, printm is working well - printing only 9 numbers. Where is error?
I think the problem is the space after %i: you do not need the tenth number, but your code is asking for it anyway, because it waits to get a space after the ninth number.
Separately, your printing code can be optimized a little by dropping the if:
for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
}
printf("\n");
}
scanf("%i ", &mat[x][y]);
Get rid of the space after %i, so it only reads a number:
scanf("%i", &mat[x][y]);

Resources