Function run 2 times.? - c

I made this program, code successfully compiled I thought that the program takes 5 inputs from user but it takes 10 inputs.
#include <stdio.h>
int greatest_number();
int main()
{
greatest_number();
printf("Greatest number is %d", greatest_number());
return(0);
}
int greatest_number()
{
int a[6], x, i, z, y;
I don't know why this loop takes 10 inputs from user
even i programmed it to take 5 inputs
for(x=0; x<5; x++)
{
printf("Enter a number:");
scanf("%d", &a[x]);
}
a[x]=0;
x=0, i=1;
y=0;
z=a[x];
while(a[x])
{
if(z>a[i]){
z=a[y];
}else {
z=a[i];
y=i;
}
x++;
i++;
}
return(z);
}

In this code snippet the function greatest_number is called twice
greatest_number();
^^^^^^^^^^^^^^^^^
printf("Greatest number is %d", greatest_number());
^^^^^^^^^^^^^^^^^
The first call is redundant and its result is discarded.
Also it seems that then all entered elements have negative values (except the last element that has as I have understood a sentinel value) then the function will return the sentinel value.

Related

How to get the square of loops in C

Using a do...while() loop need to continuously scan for random integers that will be inputted by the user and print out its square, separated in each line. Once the inputted value is 0, it will still print out its square value but should then terminate the loop afterwards.
this is the sample input:
2,
6,
0
this is the sample output:
4,
36,
0
This is my code so far:
#include <stdio.h>
int main() {
int a;
int num;
int b;
do {
scanf("%d", &a);
} while (a != 0);
while (a > num) {
num++;
if (num == a) {
b = num * a;
printf("%d", b);
}
}
}
I think this might help:
#include <stdio.h>
int main() {
int a;
do {
scanf("%d", &a);
printf("%d\n", a * a);
} while (a != 0);
}
The first issue I can see in your code is that the variable num is not initialized to a specific value, so num > a is meaningless and the second loop won't work correctly.
The second is that the a variable can store only one value at a time and that's why the value users enter will be replaced with the last value they entered, and finally, you can only print the square of a number out.
A very simple solution is that you can want the user to enter as many numbers as they want (and also 0 as the last number), press Enter at the end. After that, only call printf to print the square of them on the screen.
#include <stdio.h>
int main()
{
int a;
do
{
scanf("%d", &a);
printf("%d ", a * a);
} while(a != 0);
return 0;
}

Why does my loop terminate even with a condition?

I am trying to get user input and store them in an array Fib[i]. After that print the Fib[i] array. When the user enters -1 the loop will quit and the program will end. But my code is not printing or terminating.
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i;
for(i=0; i<50; i++)
{
scanf("%d", &Fib[i]);
if(i==-1)
break;
//printf("numbers entered %d\n", Fib[i]); // <- doesn't terminate if printf is here
}
printf("numbers entered %d\n", Fib[i]); //doesn't print anything??
}
int main()
{
int i, n;
//calling the function
fib(n);
return 0;
}
user input:
4
5
-1
Expected output:
Numbers entered
4
5
First issue: you declare Fib as an array of double:
double Fib[50];
But you use %d to read the values, which is for reading an int:
scanf("%d", &Fib[i]);
Using the wrong format specifier invokes undefined behavior. You presumably want to store integers, so change the array to int:
int Fib[50];
Next is your array breakout condition:
if(i==-1)
i is your array index, which ranges from 0 to 49, so this will never be true. You want to stop when the user enters -1, and that value will be in Fib[i]:
if(Fib[i]==-1)
Finally, printing the array:
printf("numbers entered %d\n", Fib[i]);
This doesn't print the array. It just prints the element at the last index of i, and the value at that index will always be -1. You need a separate loop to print the values:
int j;
printf("numbers entered:\n");
for (j=0; j<i; j++) {
printf("%d\n", Fib[j]);
}
This code has many code writing standard issues but it seems you are new so I am making minimal changes just for your understanding
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i,j;
for(i=0; i<50; i++)
{
scanf("%lf", &Fib[i]);
if(Fib[i]==-1)
break;
}
printf("numbers entered \n");
for(int j=0;j<i;j++)
{
printf("%lf\n",Fib[j]);
}
}
int main()
{
int i, n;
fib(n);
return 0;
}

Creating a function for a random number 2D array

So, I have this code which I need to turn into a function:
int main(void) {
int i=0,seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
printf("\nSeed value is:%d\n\n",seed);
srand(seed);
int a[5][5];
int x,y;
printf("Matrix A:\n");
for(x=0;x<5;x++) {
for(y=0;y<5;y++) {
a[x][y] = rand() %51 + (-25);
printf("%d ",a[x][y]); }
printf("\n"); }
printf("\n\n");
So basically, it produces a 2D 5x5 array of random numbers. This works fine, however my next task is applying a function to this code, with the function name of:
void generate_matrices(int a[5][5])
I have tried multiple times, the closest I got to a successful code was:
#include <stdio.h>
#include <stdlib.h>
void generate_matrices(int a[5][5]);
int main(void) {
int a, seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
srand(seed);
printf("\nSeed value is:%d\n\n",seed);
generate_matrices(a);
return 0;
}
void generate_matrices(int a[5][5]) {
int y,z;
printf("Matrix A:\n");
for(y=0;y<5;y++) {
for(z=0;z<5;z++) {
a[y][z] = rand() %51 + (-25); }
printf("%d ",a[y][z]); }
printf("\n");
}
But this returns the error, "expected 'int(*)[5]' but arguement is of type 'int'.
All/any help is muchly appreciated. To be fair on my part, I have done 90% of the code. This is the only bit I need help with so that I can apply this to the rest of my code.
Cheers!
You have declared a as a single integer on this line int a, seed;
When you call the function with generate_matrices(a); you are passing a single integer instead of a pointer to an array.
Change your declaration line to int a[5][5], seed;
generate_matrices(a); will pass a pointer to the first element in your 5 * 5 array, to the function.
You should really print the results in main and not in the function, then you will know that the array has been modified and is available for use in the body of your program.
You have used unconventional placement of braces '}' and this makes it harder to see what belongs in each part of your for loops.
You have the print statements in the wrong places - as a result only part of the matrix is printed.
This is what it should be (just the results - in main):
printf("Matrix\n ");
for (y = 0; y < 5; y++) {
for (z = 0; z < 5; z++) {
printf("%d\t ", a[y][z]);
}
printf("\n");
}
If you use int a[5][5] and call the function with generate_matrices(a);
a function void generate_matrices(int a[5][5]) {...} compiles without error
#include<stdio.h>
#include<stdlib.h>
void modify(int b[5][5]);
int main()
{
srand(4562);
int i,j,arr[5][5];
modify(arr);
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%d ",arr[i][j]=rand() %51 + (-26)); }
}
return 0;
}
void modify(int b[5][5])
{
int i,j;
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
b[i][j]; }
}
}
So this is the closest I have come to completing it. It produces the number of elements I want, also within the range I want. However its not producing the 5x5 grid I need. Where have I gone wrong?
EDIT: I'm not going for neatness at the moment, I just want to get the program working how I want it too and then i'll neaten it up.
EDIT 2: Never mind, realised what I didn't include. Its fine now. Thanks for the help.

How to save do...while status/progress in C?

I wrote this program, which returns the biggest integer inserted by the user. Now, I would like the program to return the 2nd biggest integer. I created a new variable (called "status"), which is supposed to increment 1 unit every time the cycle repeats. Then, after the break condition happens, I would step back 1 unit in status variable, in order to retrieve the 2nd biggest number. I would like to follow this line of thought (if it is reliable in C) and I ask you fellows what is wrong with my current implementation.
#include <stdio.h>
int main()
{
int x, tmp=0, status=0, bigger;
printf("Insert numbers:\n");
do{
status+=1;
scanf("%d", &x);
if (tmp>=x)
bigger=tmp;
else {
bigger=x;
tmp=x;
}
}while (x!=0);
status-=1;
printf("The second biggest number is %d.\n", bigger);
return 0;
}
If you need only two biggest integers, you can just save them in each iteration, such that you have the max and max2 values:
#include <stdio.h>
int main()
{
int x, max=0, max2=0;
printf("Insert numbers:\n");
do {
scanf("%d", &x);
if (x > max) {
max2 = max; // Save the previous max, as the second largest value
max = x ; // Save the new max
}
else if (x > max2) {
max2 = x; // The input is not max, but greater than second max
}
}while (x!=0);
printf("The second biggest number is %d.\n", max2);
return 0;
}
The status in your code is just a counter. To solve your problem :
Use an array to store all the data.
Make a variable and test it if it is different than the biggest and bigger than all other entries
try this :
int main()
{
int x, status=0, bigger=0, secondbigger=0, Array[50];
while(true){
scanf("%d",&x)
if(x==0)break;
Array[status]=x;
status++;
}
for(i=0;i<status-1;i++){
if(Array[i]>bigger) bigger=Array[i];
}
for(i=0;i<status-1;i++){
if(Array[i]>secondbigger && Array[i]!=bigger) secondbigger=Array[i];
}
printf("The second biggest number is %d.\n", secondbigger);
return 0;
}

Program will stop once five even numbers are placed in array

This is a program that gets numbers input. From the numbers given or inputted, store in an array those numbers only that are even. Input will stop/terminates once 5 even numbers are already stored in the array. So here's my code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int x, counter, even[5], numEven=0;
for(counter=0; counter<5; counter++){ //loop for getting the numbers from the user
printf("Enter number: ");
scanf("%d", &num[counter]);
if(num[counter]%2==0){ //storing the even numbers
even[numEven] = num[counter];
numEven++;
}
}
printf("\n\nEven numbers: "); //printing even numbers
for(counter=0; counter<numEven; counter++){
printf("%d, ", even[counter]);
}
getch();
return 0;
}
I have confusion in the part where will I stop the inputting when there's already 5 even numbers stored. Is there something missing? Or am I doing the wrong way? I hope I can get help and suggestions with the code. Thank you very much.
#include <stdio.h>
#include <conio.h>
int main()
{
int x, even[5], numEven = 0;
while (numEven < 5)
{
scanf("%d", &x);
if (x % 2 == 0)
{
even[numEven++] = x;
}
}
printf("\n\nEven numbers: "); //printing even numbers
for(x=0; x<numEven; x++)
{
printf("%d, ", even[x]);
}
getch();
return 0;
}
You keep readin inputs till numEven reaches 5. If the read input is an even number store it in the array and increment numEven.
Use a while loop if the number of times the program will ask the user for input is not fixed and dependent on the user's input.
while (numEven < 5) {
printf("Enter number: ");
scanf("%d", &num[counter]);
if (num[counter] % 2 == 0) {
even[numEven] = num[counter];
numEven++;
}
}

Resources