Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to know if I can call the information that is in tire_pressure in the structure and use it in another function? Basically I am not sure how to call it from one to the other. Do I have to declare the function in tireTest? Which is the function I am trying to access the tire_pressure information at. Thanks for the help
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tires
{
char Manufacturer[40];
int tire_pressure[0];
int pressure_change[0];
}typedef tires;
// Prototypes
void getTireInformation(tires*, int);
void tirePressure(tires*, int);
void tireTest(tires*, int);
int main()
{
tires tire[4];
tires* ptire = &tire[0];
srand(time(NULL));
getTireInformation(ptire, 4);
tirePressure(ptire, 4);
tireTest(ptire, 4);
return 0;
}// end main
//============================
void getTireInformation(tires* ptire, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", &(ptire + i) ->Manufacturer);
}
printf("all tire make you entered ...just for verification:\n");
for(i = 0; i < size; i++)
printf("%s\n",(ptire +i) ->Manufacturer);
}//end getTireInformation
//=======================================================================
void tirePressure(tires* ptire, int size)
{
int i = 0;
int min = 18;
int max = 35;
for (i = 0; i < size; i++)
{
(ptire + i) ->tire_pressure[0] = rand() % (max - min + 1) + min;
printf("The Tire pressure is: ");
printf("%d\n",(ptire + i) -> tire_pressure[0]);
}// end for
}// end tirePressure
//==============================================================
void tireTest(tires* ptire, int size)
{
int i = 0;
int min = 2;
int max = 5;
int change[0] = {0};
i++;
for (i = 0; i < size; i++)
{
change[0] = rand() % (max - min + 1) + min;
//(ptire + i) -> pressure_change[0] = rand() % (max - min + 1) + min;
printf("Pressure change from test %d: %d\n",i + 1, change[0]);
//(ptire + i) -> pressure_change[0] = change[0] + tirePressure;
//printf("%d\n", (ptire +i) -> pressure_change);
}// end for
}
I think you're making the problem more difficult than it is -- see if this simplification solves your problems:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PRESSURE_MIN 18
#define PRESSURE_MAX 35
struct tires
{
char Manufacturer[40];
int tire_pressure;
int pressure_change;
} typedef tires;
// Prototypes
void getTireInformation(tires *, int);
void tirePressure(tires *, int);
void tireTest(tires *, int);
//===============================================================
void getTireInformation(tires *ptire, int size)
{
for (int i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", ptire[i].Manufacturer);
}
printf("all the tire makes you entered ... just for verification:\n");
for (int i = 0; i < size; i++)
{
printf("%s\n", ptire[i].Manufacturer);
}
} // end getTireInformation
//===============================================================
void tirePressure(tires *ptire, int size)
{
for (int i = 0; i < size; i++)
{
ptire[i].tire_pressure = rand() % (PRESSURE_MAX - PRESSURE_MIN + 1) + PRESSURE_MIN;
printf("The Tire pressure is: ");
printf("%d\n", ptire[i].tire_pressure);
} // end for
} // end tirePressure
//==============================================================
void tireTest(tires *ptire, int size)
{
int min = 2;
int max = 5;
for (int i = 0; i < size; i++)
{
int change = rand() % (max - min + 1) + min;
printf("Pressure change from test %d: %d\n", i + 1, change);
ptire[i].pressure_change = change + ptire[i].tire_pressure;
printf("%d\n", ptire[i].pressure_change);
} // end for
}
//===============================================================
int main()
{
tires tire[4];
srand(time(NULL));
getTireInformation(tire, 4);
tirePressure(tire, 4);
tireTest(tire, 4);
return 0;
} // end main
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've got a problem. I've tried to write program. This is command:
The user specifies a whole number n>0.
Program:
Allocates two arrays of numbers of type int size n+1
Using only these arrays and a small number of statically allocated variables, the program calculates recursively the n line of the Pascal triangle (all binomial symbols with an upper parameter equal to n)
Prints out the calculated values
Memory slowing down
Example
input: 5
output: 1 5 10 10 5 1
I wrote iteration, but I have no idea how change this for recursion.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,k;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0') printf("%d", 1);
if(n=='1') printf("%d %d", 1, 1);
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = 1;
array_1[1] = 1;
k=1;
while(k!=n)
{
for(i=0; i<=k+1; i++)
{
if(i==0)
{
array_2[0] = 1;
}
else if(i==n)
{
array_2[i] = 1;
}
else
{
array_2[i] = array_1[i] + array_1[i-1];
}
}
for(i=0; i<=n; i++)
{
array_1[i] = array_2[i];
array_2[i] = 0;
}
k++;
}
for(i=0; i<=n; i++)
{
printf("%d ", array_1[i]);
}
free(array_1);
free(array_2);
return 0;
}
The recursive version could look something like the following, with the actual work being left to fill-in under the two /* ... */ comments. The missing code essentially exists in the iterative version as posted, it just needs to be retrofitted here.
void recurse(int k, int n, int *array_1, int *array_2)
{
/*
print previously calculated k-th row in array_1
*/
// nothing left to do
if (k == n + 1) return;
/*
calculate next (k+1)-th row in array_2
*/
// swap arrays and repeat
recurse(k + 1, n, array_2, array_1);
}
int main()
{
int n, *array_1, *array_2;
if(scanf("%d", &n) != 1) return 1; // input error
if (n < 0) return 1; // invalid input
array_1 = (int*)calloc(n + 1, sizeof(int));
array_2 = (int*)calloc(n + 1, sizeof(int));
array_1[0] = 1;
recurse(1, n, array_1, array_2);
free(array_1);
free(array_2);
return 0; // done
}
Thanks everyone for answer :). This is my code:
#include <stdio.h>
#include <stdlib.h>
void recurse (int k, int n, int *array_1, int *array_2)
{
int i;
if(k==n+1) return;
for(i=1; i<=k+1; i++) array_2[i] = array_1[i] + array_1[i-1];
recurse(k+1, n, array_2, array_1);
}
void output(int n, int *array_1, int *array_2)
{
int i;
if(n%2!=0)
for(i=0; i<=n; i++) printf("%d ", array_1[i]);
else
for(i=0; i<=n; i++) printf("%d ", array_2[i]);
}
int main()
{
int n;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0')
{
printf("%d", 1);
return 0;
}
else if(n=='1')
{
printf("%d %d", 1, 1);
return 0;
}
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = array_1[1] = array_2[0] = 1;
recurse(1, n, array_1, array_2);
output(n, array_1, array_2);
free(array_1);
free(array_2);
return 0;
}
The random function is not working in the parameters set and I do not know why. Can anyone help? I need random numbers between 18 and 38 and I can't seem to get that and I do not know why.
Here's my code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tires
{
char Manufacturer[40];
int tire_pressure[2];
int pressure_change;
}typedef tires;
void getTireInformation(tires*, int);
void tirePressure(tires*, int);
int main()
{
tires tire[4];
tires* ptire = &tire[0];
getTireInformation(ptire, 4);
tirePressure(ptire, 4);
return 0;
}
void getTireInformation(tires* ptire, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", &(ptire + i) ->Manufacturer);
}
printf("all tire make you entered ...just for verification:\n");
for(i = 0; i < size; i++)
printf("%s\n",(ptire +i) ->Manufacturer);
}
void tirePressure(tires* ptire, int size)
{
int i = 0;
int min = 18;
int max = 38;
for (i = 0; i < size; i++)
{
srand(time(NULL));
ptire = rand()%(max - min)-min;
printf("%d\n", (ptire + i) -> tire_pressure);
}
}
Edit: Here's my updated function after making the suggested fixes
void tirePressure(tires* ptire, int size)
{
int i = 0;
int min = 18;
int max = 38;
for (i = 0; i < size; i++)
{
ptire = rand()%(max - min + 1) + min;
printf("%d\n", (ptire + i) -> tire_pressure);
}
}
It's not necessary to call srand(time(NULL)); every time it generates a random number. Put that in main(), before any function call.
Then change
rand() % (max - min) - min;
to
rand() % (max - min + 1) + min;
Say max = 3 and min = 1, you need rand() % 3 + 1 to generate a random number from 1 to 3 inclusively.
There is another problem, which have nothing to do with random number generating: The random numbers generated is assigned to ptire, that is, you are assigning a tires* with an int!
I've refined your code. Hope it will work:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tires
{
char Manufacturer[40];
int tire_pressure[2];
int pressure_change;
} typedef tires;
// Prototypes
void getTireInformation(tires*, size_t);
void tirePressure(tires*, size_t);
int main()
{
tires tire[4];
tires* ptire = &tire[0];
srand(time(NULL));
getTireInformation(ptire, 4);
tirePressure(ptire, 4);
return 0;
}
void getTireInformation(tires* ptire, size_t size)
{
size_t i = 0;
for (i = 0; i < size; i++)
{
printf("Please enter the maker of the tire: \n");
scanf("%s", (ptire + i) -> Manufacturer); // just use str. &str actually causes undefined bahavior
}
printf("All tire make you entered ...just for verification:\n");
for(i = 0; i < size; i++)
printf("%s\n", (ptire +i) -> Manufacturer);
}
void tirePressure(tires* ptire, size_t size)
{
int i = 0;
int min = 18;
int max = 38;
for (i = 0; i < size; i++)
{
(ptire + i) ->tire_pressure[0] = rand() % (max - min + 1) + min;
printf("%d\n", (ptire + i) -> tire_pressure[0]);
}
}
And here is the result when I run it:
Please enter the maker of the tire:
qwert
Please enter the maker of the tire:
fewqwe
Please enter the maker of the tire:
hcgexf
Please enter the maker of the tire:
zrbghcr
All tire make you entered ...just for verification:
qwert
fewqwe
hcgexf
zrbghcr
22
34
31
31
All numbers are between 18 to 38 now. Note that tire_pressure is an array containing two ints. Without knowing your purpose, I just gave random numbers to its first element.
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].
I updated my main and sequetialSearch and now it crashes when it runs. It compiles okay, but then crashes.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "percentage.h"
#include "sequentialSearch.h"
#define searchAmount 100
int main(int argc, char *argv[])
{
int numbers[100];
int searches[searchAmount];
int testAmounts[searchAmount];
int i;
int where;
int searchSuccess;
int searchUnsuccess;
int percent;
int looker;
int sum;
int average;
srand(time(NULL));
for (i = 0; i < 100; i++){
numbers[i] = rand() % 200;
}
for (i = 0; i < searchAmount; i++){
searches[i] = rand() % 200;
}
searchUnsuccess = 0;
searchSuccess = 0;
sum = 0;
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searchSuccess++;
testAmounts[i] = looker;
}else{
searchUnsuccess++;
testAmounts[i] = looker;
}
}
for(i = 0; i < searchAmount; i++){
sum = sum + testAmounts[i];
}
average = sum / searchAmount;
percent = percentRate(searchSuccess, searchAmount);
printf("Total number of searches: %d\n", searchAmount);
printf("Total successful searches: %d\n", searchSuccess);
printf("Success Rate: %d%%\n", percent);
printf("Total number of tests ran: %d\n", average);
system("PAUSE");
return 0;
}
sequentialSearch.h
bool seqSearch (int list[], int last, int target, int* locn, int* looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
*looker++;
}
*locn = *looker;
return(target == list[*looker]);
}
Pass looker in by reference, so that you can access its value from the caller.
int looker;
...
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searches[i] += looker;
searchSuccess++;
}else{
searchUnsuccess++;
}
}
bool seqSearch (int list[], int last, int target, int* locn, int *looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
(*looker)++;
}
*locn = *looker;
return(target == list[*looker]);
}
By the way, you may wish to reconsider defining functions in your header file; this could cause problems with duplicate symbol when linking if you have more than one c file including this file.
Why not just pass looker in as an int*, use it essentially as you have been, look at the value after seqSearch(...) returns, and add it to a running total back in main()?
One problem is that the increment of looker in seqSearch is incrementing the pointer rather than the value. It should probably be:
(*looker)++;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void initDeck (int deck[]);
void showDeck (int deck[]);
void shuffleDeck (int deck[]);
int getBet ();
main()
{
int deck[52];
int playerBet;
char z;
initDeck(deck);
shuffleDeck(deck);
showDeck(deck);
playerBet = getBet();
//scanf ("%d\n", &playerBet);
printf("%d\n", playerBet);
z = 1;
getchar(z);
return 0;
}
void initDeck (int deck[]){
int k;
int i;
for (k = 1; k < 53; k++){
i = k - 1;
deck[i] = k;
}
return;
}
void showDeck (int deck[]){
int k;
for (k = 0; k < 52; k++){
printf("%d\n", deck[k]);
}
return;
}
void shuffleDeck (int deck[]){
int random;
int k;
int temp;
srand(time(0));
for (k = 52; k > 1; k--){
random = (rand() % k) + 1;
if (random != k){
temp = deck[k - 1];
deck[k - 1] = deck[random - 1];
deck[random- 1] = temp;
}
else{
k++;
continue;
}
}
return;
}
int getBet (){
int bet;
scanf ("%d\n", &bet);
return bet;
}
The function at issue is getBet() and when I input an integer it doesn't give me any output. I tried doing the input in main and it worked, but I don't see the problem with this. I've double checked for small errors a few times, and I don't see anything wrong with it...
The problem is that you end your scanf string with a newline. This means (read the scanf documentation) any amount of whitespace. So when you enter "" it still waits for more white space. Try entering non-whitespace characters afterwards to see it accept the input. As Artem says, omitting the \n could be one solution.
Instead of
scanf("%d\n", &bet);
do
scanf("%d", &bet);
Just tested and it works.
I also don't see the error. Why don't you pass it by address instead?
int main()
{
int playerBet;
//
getBet(&playerBet);
}
void getBet(int* bet)
{
scanf("%d", bet);
}
I don't do C, but that is the general idea.