Ordinal number of the smallest number without array - c

I need help solving this problem, if anyone had a similar problem it would help me a lot.
The task is:
Write a program that loads n numbers and then prints the smallest as well as its ordinal number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,d,m,min=1;
printf("Enter as many numbers as you want:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&m);
if (i == 1)
{
min = m;
}
else if (m < min)
min = m;
}
printf("Min is: %d",min);
}
Thanks in advance !

Related

int , float problems in c language

can some one please explain for me please why i have proplems with the answer when i Enter 2 kined of date type int and float , for example : to fined the minimum element in 2D-array matrix :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,m,n;
float min,x[10][10];
puts(" Please Enter the m & n:");//rows & columns
scanf("%d %d",&m,&n);
min = x[0][0];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf(" Please Enter x[%d][%d]" ,i,j);
scanf("%8.5f",&x[i][j]);
if ( x[i][j] < min )
{
min = x[i][j];
}
}
printf(" minimum in is %8.2f \n", min);
return 0;
}
but if i use int onlye there will be no problems with the answer ?
i try to use both type of data but its got wrong answers .

Can't find the error in minimum difference between two numbers on array in C

So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}

How do I store the value of the max integer in an array before repeating loop?

It's been a couple hours. I don't understand arrays well enough to determine the right way to save a value as max and then determine the maximum value within the array. Please help explain how to make the for loop correctly. Thanks.
#include <stdio.h>
#include <conio.h>
#define SIZE 3
int main (void){
int max;
int min;
int myArray[SIZE];
int count;
printf("Please enter integers\n");
for (count=0;count<=SIZE;count++){
scanf("%d",&myArray[count]);
}
for (count=0;count<=SIZE;count++){
printf("The values of myArray are %d\n",myArray[count]);
}
for (count=0;count<=SIZE;count++){
max=myArray[0];
if (myArray[count]>max){
myArray[count]=max;
}
}
printf("The largest is %d\n",max);
printf ("The smallest is %d\n",min);
getch();
return 0;
}
max=0;
for (count=0;count<SIZE;count++){
if (myArray[count]>max){
max = myArray[count];
}
}
You need change all <= SIZE to < SIZE

Problems with arrays

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);

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