#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;
}
Related
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;
}
I'm doing a program that check if 5 numbers that the user insert are even or odd, then they will be stored into an array and finally these values will be printed out on screen. In order to do this i've divided this program in two functions just to understand how the functions and the arrays works together, but it doesn't print the values that i've putted in. Why?
int check_even_and_odd(int number, int list[]){
printf("Insert the numbers\n");
scanf("%d", &list[number]);
if (number % 2 == 0) {
printf("Even\n");
}
else{
printf("Odd\n");
}
return 0;
}
int main () {
int k;
int i = 0;
int list2[5] = {0};
while (i < 5) {
i++;
k = check_even_and_odd(i, &list2[i]);
}
i = 0;
while (i < 5) {
i++;
printf("\n%d\n", list2[i]);
}
return 0;
}
Edit: Now that the main issue is gone, I want to add a little improvement to this little project. I want that the program tells to me how many Even or Odd number are in the array, but i don't know how to do it. I was thinking about adding 2 counters into the if statement (one for the even number and one for the odd numbers) but once i do this i don't know how to continue.
The program with the counters is this:
void check_even_and_odd(int number, int list[]){
int even = 0;
int odd = 0;
printf("Insert the numbers\n");
scanf("%d", &list[number]);
if (number % 2 == 0) {
even++;
}
else{
odd++;
}
printf("Even numbers are: %d\n", even);
printf("Odd numbers are: %d\n", odd);
}
int main () {
int i = 0;
int list2[5] = {0};
while (i < 5) {
i++;
check_even_and_odd(i, list2);
}
i = 0;
while (i < 5) {
i++;
printf("\n%d\n", list2[i]);
}
return 0;
}
Obviously it isn't complete, but as i have already said, i don't know how to continue
Your function expects an array argument but you are passing the address of individual elements of the array, so it won't work properly, you'll just need to use the correct argument:
k = check_even_and_odd(i, list2);
Quibble: k is never used so you don't really need it. You can just make your function void and remove the variable:
void check_even_and_odd(int number, int list[]){
printf("Insert the numbers\n");
scanf("%d", &list[number]);
if (number % 2 == 0){
printf("Even\n");
}
else{
printf("Odd\n");
}
}
int main(){
int i = 0;
int list2[5] = {0};
while (i < 5){
i++;
check_even_and_odd(i, list2);
}
i = 0;
while (i < 5){
i++;
printf("\n%d\n", list2[i]);
}
return 0;
}
Your fault is in line scanf("%d", &list[number]); just need to change it to scanf("%d", &list); but i think you are miss understanding whole array and pointer logic. You can't pass list as argument and if you do that, The compiler will changed it to pointer automatically. So if you want to tell a function about your list you just have to pass it your list address in memory (pointer). So you should do it like:
#include <stdio.h>
int How_Many_Odd = 0;
int How_Many_Even = 0;
void Add_To_List(int Number, int *ListIndex){
printf(
"Number %d is %s\n",
Number,
(Number % 2 == 0)? "Even": "Odd" // check if is odd or even
);
if(Number % 2 == 0)
How_Many_Even++;
else
How_Many_Odd++;
// changing value of pointer ListIndex to Number
*ListIndex = Number;
}
int main(){
// first creating integer array with size of 5
int List[5];
for(int i=0; i < 5; i++){
// waiting for user to enter number
int value;
scanf("%d", &value);
// changing value of index 0 to 3
Add_To_List(value, &List[i]);
}
// showing how many odds and how many evens
printf("%d numbers are even and %d numbers are odd\n", How_Many_Even, How_Many_Odd);
// you can show every index value too
for(int i=0; i < 5; i++)
printf("value of index %d is %d\n", i, List[i]);
return 0;
}
I recommend you to learn about pointer that will fix your issues
Here is your code fixed:
#include <stdio.h>
void check_even_and_odd(int number, int list[], unsigned int *even_count)
{
printf("Insert the numbers\n");
scanf("%d", &list[number]);
if (number % 2 == 0) {
printf("Even\n");
*even_count += 1;
}
else
{
printf("Odd\n");
}
}
int main()
{
unsigned int even_count;
int i = 0;
int list2[5] = {0};
while (i < 5)
{
check_even_and_odd(i, list2, &even_count);
i++;
}
i = 0;
while (i < 5)
{
printf("\n%d\n", list2[i]);
i++;
}
printf("There are %d even numbers and %d odd ones.\n", even_count, 5 - even_count);
return 0;
}
Basically, your main problem is in passing the list to the function in k = check_even_and_odd(i, &list2[i]);, as you should be passing the entire list, not a specific number in the list.
regarding:
if (number % 2 == 0) {
This is checking the passed in parameter rather than the value entered by the user. Suggest:
...
if( (list[number] % 2) == 0 )
{
printf( "%s\n", "number is even" );
....
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 !
Can't get my program to output the correct number. I feel like I am making a simple mistake. This is written in C.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int list[n];
while(1)
{
scanf("%d", &n);
if(n == -1)
{
break;
}
else
{
for(i = 2; i < n; i++)
{
list[i] = list[i-1]+list[i-2];
}
printf("%d %d", i, list[i] );
}
}
}
(To make things simpler, I'm going to ignore dealing with input.)
First problem is turning on compiler warnings. Most C compilers don't give you warnings by default, you have to ask for them. Usually by compiling with -Wall. Once we do that, the basic problem is revealed.
test.c:6:14: warning: variable 'n' is uninitialized when used here [-Wuninitialized]
int list[n];
^
test.c:5:10: note: initialize the variable 'n' to silence this warning
int n, i;
^
= 0
1 warning generated.
int list[n] immediately creates a list of size n. Since n is uninitialized it will be garbage. You can printf("%d\n", n); and see, it'll be something like 1551959272.
So either n needs to be initialized, or you need to reallocate list dynamically as n changes. Dynamic allocation and reallocation gets complicated, so let's just make it a static size.
So we get this.
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Allocate an array of MAX_N integers */
const int MAX_N = 10;
int list[MAX_N];
/* Do Fibonacci */
for(int i = 2; i < MAX_N; i++) {
list[i] = list[i-1]+list[i-2];
}
/* Print each element of the list and its index */
for( int i = 0; i < MAX_N; i++ ) {
printf("%d\n", list[i]);
}
}
That runs, but we get nothing but zeros (or garbage). You have a problem with your Fibonacci algorithm. It's f(n) = f(n-1) + f(n-2) with the initial conditions f(0) = 0 and f(1) = 1. You don't set those initial conditions. list is never initialized, so list[0] and list[1] will contain whatever garbage was in that hunk of memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Allocate an array of MAX_N integers */
const int MAX_N = 10;
int list[MAX_N];
/* Set the initial conditions */
list[0] = 0;
list[1] = 1;
/* Do Fibonacci */
for(int i = 2; i < MAX_N; i++) {
list[i] = list[i-1]+list[i-2];
}
/* Print each element of the list and its index */
for( int i = 0; i < MAX_N; i++ ) {
printf("%d\n", list[i]);
}
}
Now it works.
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
Here is code snippet,
#include <stdio.h>
int main()
{
int MAX_SIZE = 100; //Initial value
int n, i;
int list[MAX_SIZE];
printf("Enter value of 'n'");
scanf("%d",&n);
if(n < 0){
printf("'n' cannot be negative number");
return 0;
}else if (n==1){
list[0]=0;
}else if(n == 2){
list[0]=0;
list[1]=1;
}else{
list[0]=0;
list[1]=1;
for(i = 2; i <= n; i++)
{
list[i] = list[i-1]+list[i-2];
}
}
//To view array elements
for(int i=0;i<n;i++){
printf("%3d",list[i]);
}
}
You don't have return in main function.
n must be defined previous. Otherwise it took random value from memory.
So, your list array is created with unknown value.
int list[n];
Also, this will never happends, becous n is declared, but not defined.
i < n;
Is this what you need?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int F[100];
F[0] = 0;
F[1] = 1;
int i = 2;
while(1)
{
if(i < 100)
{
F[i] = F[i-1] + F[i-2];
i++;
}
else
{
break;
}
}
i = 0;
while(1)
{
if(i < 100)
{
printf("%d ; ", F[i]);
i++;
}
else
{
break;
}
}
return 0;
}
You need to allocate memory on demand for each iteration. In your code, n is uninitalized which leads to unpredectiable behavior. Also you need to initialize list[0] and list[1] since this is the 'base' case.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int* list; /* Declare a pointer to the list */
while(1)
{
scanf("%d", &n);
if(n == -1)
{
break;
}
else if ( n > 0 )
{
list = (int *) malloc( n * sizeof(int) );
list[0] = 1;
list[1] = 1;
for(i = 2; i < n; i++)
{
list[i] = list[i-1]+list[i-2];
}
printf("%d %d\n", i, list[i-1] );
free(list);
}
}
}
I am trying to implement a simple tournament in C.
#include <stdio.h>
int main(void) {
int tourn[100], n, i;
printf("Give n:");
scanf("%d", &n);
printf("\n n = %d \n", n);
for(i = n; i <= (2*n)-1; i++)
scanf("%d", &tourn[i]);
build(tourn, n);
printf("\n Max = %d \n",tourn[1]);
printf("\n Next Max = %d \n",nextmax(tourn, n));
}
void build(int tourn[], int n) {
int i;
for(i = 2*n-2; i > 1; i = i-2)
tourn[i/2] = max(tourn[i], tourn[i+1]);
}
int nextmax(int tourn[],int n) {
int i = 2;
int next;
next = min(tourn[2], tourn[3]);
while(i <= 2*n-1) {
if(tourn[i] > tourn[i+1]) {
next = max(tourn[i+1], next);
i = 2*i;
}
else {
next = max(tourn[i], next);
i = 2*(i+1);
}
}
return(next);
}
int max(int i,int j) {
if(i > j)
return i;
else
return j;
}
int min(int i,int j) {
if(i < j)
return i;
else
return j;
}
The output for n = 5 and
1 2 3 4 5
is
Max = 4195048
Next Max = 32588
and this output varies each time by a small amount!
if I place a test printf command before the build function, it doesn't execute.
Can someone find the error/explain the output?
Thanks :)
Your code seems pretty broken to me. you don't mind to address beyond your array boundaries, which is a good way of producing random results:
while(i <= 2*n-1){
if(tourn[i]>tourn[i+1]){
next = max(tourn[i+1],next);
i=2*i;
} else {
next = max(tourn[i],next);
i=2*(i+1);
}
}
Your (logical) array is of size 2n. if i reaches the "highest" value, you test tourn[i + 1], which is tourn[2n].