I'm trying to work on this assignment that prints "*" in their respective number. Also, if you see the picture I need to display the min and the max number, how can I do this? I was thinking about doing it with an array storing those numbers there but when I created the array and tried to pass them I got an error.
Here's the picture:
#include <stdio.h>
#include <limits.h>
#include <math.h>
int f(int);
int main(void){
int i, t, funval,tempL,tempH;
int a;
// Make sure to change low and high when testing your program
int low=-3, high=11;
for (t=low; t<=high;t++){
printf("f(%2d)=%3d\n",t,f(t));
}
printf("\n");
printf(" ");
for (i=1; i<=31; i+=5)
printf("%3d ", i);
printf("\n");
printf(" ");
for (i=1; i<=31; i+=5)
printf(" | ");
printf("\n");
for (t=low; t<=high;t++){
printf("t=%2d\n",t);
}
printf("\n");
for(i=0;i<=sizeof(nums)/sizeof(int);i++){
if (nums[i] > max)
{
max = nums[i];
}
if (nums[i] < min)
{
min = nums[i];
}
}
printf("Min: %d\n", min);
printf("Max: %d\n", max);
printf("\n");
printf(" ");
for (i=min; i<=max; i+=5)
printf("%3d ", i);
printf("\n");
printf(" ");
for (i=min; i<=max; i+=5)
printf(" | ");
printf("\n");
for (t=low; t<=high;t++){
printf("t=%2d\n",t);
}
// Your code here...
return 0;
}
int f(int t){
// example 1
return (t*t-4*t+5);
// example 2
// return (-t*t+4*t-1);
// example 3
// return (sin(t)*10);
// example 4
// if (t>0)
// return t*2;
// else
// return t*8;
}
#include <stdio.h>
#include <stdlib.h>
int parabola1(int);
int *calc(int low, int high, int (*f)(int), int *size, int *min, int *max){
/*
#input
low, high : range {x| low <= x <= high}
f : function
#output
*size : Size of array
*min : Minimum value of f(x)
*max : Maximum value
return : pointer to first element of int num[*size]
NULL if this can not be ensured.
*/
int i, x, *nums;
*size = high - low + 1;
*max = *min = f(low);//value of the provisional
if(NULL==(nums=malloc(*size*sizeof(*nums)))){
return NULL;//max and min are unavailable
}
for(i = 0, x = low; x <= high; ++x, ++i){
nums[i] = f(x);
if(nums[i] > *max)
*max = nums[i];
if(nums[i] < *min)
*min = nums[i];
}
return nums;
}
int main(void){
int i, t;
int *nums, size;
int low=-3, high=9, min, max;
nums = calc(low, high, parabola1, &size, &min, &max);
for (i=0; i<size;i++){
printf("f(%2d)=%3d\n", low+i, nums[i]);
}
printf("--\n");
printf("min=%3d\n", min);
printf("max=%3d\n", max);
printf("--\n");
t = -2;
printf("t=%2d%*s%c\n", t, nums[t-low]-min, "", '*');
free(nums);
return 0;
}
int parabola1(int t){
return t*(t-4)+5;
}
Related
I have written a piece of code below, that ask a user input a number N (3 < N < 1.000.000) odd and count the number of prime number pairs whose sum is equal to N. That code works, but I need to optmize it to make more efficient. When the user inputs a number, I count the number of primes up to this number and store each prime number in a vector, and after that count the number of prime number pairs whose sum is equal to this input.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void seleciona_primos (int *vet, int n, int raiz){
int i, j;
vet[2] = 2;
for(i=3; i<=n; i+=2){
vet[i] = i;
}
for (i=3; i<= raiz; i+=2){
if(vet[i]!=0){
for(j=3; j<=n; j+=2){
if ((vet[i]!=vet[j]) && (vet[j]%vet[i] == 0)){
vet[j]=0;
}
}
}
}
}
int conta_pares (int *vet, int n){
int i, j, count=0;
for (i=3; i<=n; i+=2){
if(vet[i]!=0){
for(j=3; j<=n/2; j+=2){
if((vet[j]!=0) && (vet[i] + vet[j] == n)&& (vet[i]!=0)){
//printf("%d ", vet[i]);
//printf("%d\n", vet[j]);
count++;
vet[i] = 0;
}
}
}
}
if(vet[n-2]!=0){
count++;
}
return count;
}
int main()
{
int *vet, n, raiz, i , count;
scanf("%d", &n);
vet = (int *) calloc((n+1), sizeof(int));
raiz = floor(sqrt(n));
seleciona_primos(vet, n, raiz);
count = conta_pares(vet, n);
printf("%d",count);
//for(i=3; i<=n; i+=2){
//if(vet[i]!=0){
//printf("%d\n", vet[i]);
//}
//}
return 0;
}
I make an array who countain the prime numbers from 2 to N(1 has only one divisors and 0 has infinites of divisors), and then I check if two numbers from the array equal to N
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool Prime(int);
int main()
{
long N;
do
{
printf("Give me the number of numbers :");
scanf("%ld",&N);
}while(N<=4||N>1000000);//N must be between 3 & 1000000
int prime[N];
int arr[N];
int j=0;
for(int i=2;i<N;i++)
{
if(Prime(i)==true)
{
prime[j]=i;
j++;
}
}
printf("\n\n");
for(int p=0;p<j-1;p++)
{
for(int q=p+1;q<j;q++)
{
if(N==prime[p]+prime[q])
{
printf("\n%d = %d + %d \n",N,prime[p],prime[q]);
}
}
}
printf("\n\n");
return 0;
}
bool Prime(int n)
{
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
Example :
Input : N =100;
Output :
100 = 3 + 97
100 = 11 + 89
100 = 17 + 83
100 = 29 + 71
100 = 41 + 59
100 = 47 + 53
Thank you for all suggestions!!
And after some research and tries, I have found the solution that follow bellow:
int isPrime(int x)
{
int i;
for(i = 2; i <= sqrt(x); i++)
{
if(x%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int i, count = 0, n ;
scanf("%d", &n);
for(i = 3; i <= n/2 ; i+=2)
{
if(isPrime(i))
{
if((isPrime(n-i)))
{
count++;
}
}
}
if(isPrime(n-2))
{
count++;
}
printf("%d", count);
return 0;
}
I modified your code to optimize it, I have set two Boolean vectors one for 5 mod(6) prime numbers (vet1[i]=false corresponds to prime number 6 x i-1 es. i=1 corresponds to prime number 5=6 x 1-1) and one (vet2[i]corresponds to prime number 6 x i+1) for prime numbers 1 mod(6)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
void seleciona_primos (bool *vet1, bool *vet2,int n){
int i, i1,imax;
imax=(n-n%6)/6+1;
for (i=1; (6*i-1)*(6*i-1) <=n; i++){
if (vet1[i]==false){
for(int i1 = 6*i*i; i1 <= imax+2*i; i1 += (6*i-1)){
if(i1<imax)
vet1[i1]=true;
vet2[i1-2*i]=true;
}
}
if (vet2[i]==false){
for(int i1 = 6*i*i; i1 <= imax; i1 += (6*i+1)){
vet1[i1]=true;
if(i1<imax-2*i)
vet2[i1+2*i]=true;
}
}
}
}
int conta_pares (bool *vet1, bool *vet2, int n){
int i,imax, count=0,r6;
imax=(n-n%6)/6;
r6=n%6;
if (r6==0){
for (i=1; i<imax; i++){
if(vet1[i]== false && vet2[imax-i]== false)
count++;
}
}
if (r6==2){
for (i=1; i<imax; i++){
if(vet2[i]== false && vet2[imax-i]== false)
count++;
}
count=(count+count%2)/2;
}
if (r6==4){
for (i=1; i<=imax; i++){
if(vet1[i]== false && vet1[imax+1-i]== false)
count++;
}
count=(count+count%2)/2;
}
if (r6>0 && r6<3){
if (vet1[imax]==false)
count++;
}
if (r6>2 && r6<5){
if (vet2[imax]==false)
count++;
}
return count;
}
int main()
{
int n, i , count;
bool *vet1, *vet2;
scanf("%d", &n);
vet1 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
vet2 = (bool *) calloc((n-n%6)/6+2, sizeof(bool));
seleciona_primos(vet1,vet2, n);
count = conta_pares(vet1,vet2, n);
printf("%d",count);
free(vet1);
free(vet2);
return 0;
}
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
I want to write a program that reads 10 int values from the user and swaps the largest and smallest numbers on the first and second values, then the rest of the numbers should be in the order.
Please check the code and help me what the wrong is.
For instance:
1
9
4
5
6
7
8
2
4
5
New order should be 9 1 4 5 6 7 8 2 4 5
#include <stdio.h>
int main() {
int a[10],i,min,max=0,pos=0;
printf("Please enter 10 int values :\n");
do{
scanf("%d", &a[pos++]);
} while (pos<10);
for (i=0; i<10;i++) {
printf("%i\n",a[i]);
if (max<a[i])
{
max=a[i];
}
if (min>a[i])
{
min=a[i];
}
for (i=0;i<10;i++) {
if (a[i]==max)
a[i]=max;
if (a[i] == min) a[i] = min;
}
printf("The new order is : %d %d %d ", max, min, ...);
return 0;
}
EDIT:
It is the new form
#include <stdio.h>
int main() {
int a[10],i,pos,temp,min = 0,max = 0;
printf("Please enter 10 int values :\n");
do {
scanf("%d", &a[pos++]);
} while (pos < 10);
for ( =1; i<10;i++) {
if (a[i]>a[max])
{
max=i;
}
if (a[i]<a[min])
{
min=i;
}
}
temp=a[max];
a[max]=a[min];
a[min]=temp;
printf("%d %d",a[max],a[min]);
for (i=0;i<10;i++){
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
return 0;
}
As others have noted, your code does not properly identify the maximum and minimum values in the array because you are writing min and max back into the array instead of the other way around.
Since you want to swap these values, what you actually want are the indices of the min and max values of the array, and swap those.
It is best to break this code into functions instead of having everything in main. Here is a solution that will do what you want:
#include <stdio.h>
int indexofmax(int *data, int len)
{
int max = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]>data[max]) max = i;
}
return max;
}
int indexofmin(int *data, int len)
{
int min = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]<data[min]) min = i;
}
return min;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
// user enters in 10 ints...
int max = indexofmax(a, 10);
int min = indexofmin(a, 10);
int i;
swap(&a[min], &a[max]);
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
This initialization min=0,max=0 is not right.
Instead have min = INT_MAX and max = INT_MIN.
By setting min=0, you would never get the lowest number in the array if it is greater than 0.
Similarly by setting max=0, you would never get the greatest number in the array if it is lower than 0.
You are gaining nothing by this code:
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
It is evident that this loop
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
does not make sense.
Moreover variable min is not initialized while variable max is initialized incorrectly.
int a[10],i,min,max=0,pos=0;
For example the array can contain all negative elements. In this case you will get incorrect value of the maximum equal to 0.
And I do not see where the elements are moved to the right to place the maximum and the minimum to the first two positions of the array.
If I have understood correctly then what you need is something like the following. To move the elements you could use standard function memmove declared in header <string.h>. However it seems you are learning loops.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 4, 5, 9, 6, 7, 1, 8, 2, 4, 5 };
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
size_t min = 0;
size_t max = 0;
for (size_t i = 1; i < N; i++)
{
if (a[max] < a[i])
{
max = i;
}
else if (a[i] < a[min])
{
min = i;
}
}
if (max != min)
{
int min_value = a[min];
int max_value = a[max];
size_t j = N;
for (size_t i = N; i != 0; --i)
{
if (i - 1 != min && i - 1 != max)
{
if (i != j)
{
a[j - 1] = a[i - 1];
}
--j;
}
}
a[--j] = min_value;
a[--j] = max_value;
}
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
}
The program output is
4 5 9 6 7 1 8 2 4 5
9 1 4 5 6 7 8 2 4 5
You're not actually altering the array.
In the second loop, you say "if the current element is the max, set it to the max". In other words, set it to its current value. Similarly for the min.
What you want is to swap those assignments.
if(a[i]==max) a[i]=min;
if(a[i]==min) a[i]=max;
Also, your initial values for min and max are no good. min is unitialized, so its initial value is undefined. You should initialize min to a very large value, and similarly max should be initialized to a very small (i.e. large negative) value.
A better way to do this would be to keep track of the index of the largest and smallest values. These you can initialize to 0. Then you can check a[i] > a[max] and a[i] < a[min]. Then you print the values at indexes min and max, then loop through the list and print the others.
int i, temp, min=0, max=0;
for (i=1; i<10; i++) {
if (a[i] > a[max]) max = i;
if (a[i] < a[min]) min = i;
}
printf("%d %d ", a[max], a[min]);
for (i=0; i<10; i++) {
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
Just keep it nice and simple, like this:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 10
int find_biggest(int A[], size_t n);
int find_smallest(int A[], size_t n);
void print_array(int A[], size_t n);
void int_swap(int *a, int *b);
int
main(void) {
int array[MAXNUM], i, smallest, biggest;
printf("Please enter 10 int values:\n");
for (i = 0; i < MAXNUM; i++) {
if (scanf("%d", &array[i]) != 1) {
printf("invalid input\n");
exit(EXIT_FAILURE);
}
}
printf("Before: ");
print_array(array, MAXNUM);
smallest = find_smallest(array, MAXNUM);
biggest = find_biggest(array, MAXNUM);
int_swap(&array[smallest], &array[biggest]);
printf("After: ");
print_array(array, MAXNUM);
return 0;
}
int
find_biggest(int A[], size_t n) {
int biggest, i, idx_loc;
biggest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] > biggest) {
biggest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
int
find_smallest(int A[], size_t n) {
int smallest, i, idx_loc;
smallest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] < smallest) {
smallest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
void
print_array(int A[], size_t n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
void
int_swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
My program is giving me two minimum values and no maximum values. My program is supposed to print max value if opcode is 1 and min value if opcode is 0. Help please
#include <stdio.h>
int minmax(int array[], int array_size, int opcode);
int main(void)
{
int array[]= {99,4,95,2,98}; //array size
int array_size = 5;
int i;
int opcode;
array_size = 5;
for (i = 0; i < array_size; i++)
{
printf("array[%d] = %d\n", i , array[i]);
}
printf("Enter an opcode 0 or 1: ");
scanf("%d", &opcode);
minmax(array, array_size, opcode);
}
int minmax(int array[], int array_size, int opcode)
{
int i;
int max = array[0];
int min = array[0];
for (i = 0; i < array_size; i++)
{
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
else if (opcode == 0 && array[i] < min)
{
min = array[i];
printf("The min value is: %d\n", min);
}
}
return 0;
}
It does not print the maximum because the first element itself is the maximum.
That is,
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
never executes in this code.
The remedy to be output the minimum or maximum at the end of the minmax() function.
Your code should look like:
int minmax(int array[], int array_size, int opcode)
{
int i;
int max = array[0];
int min = array[0];
for (i = 0; i < array_size; i++)
{
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
else if (opcode == 0 && array[i] < min)
{
min = array[i];
printf("The min value is: %d\n", min);
}
}
//The below code prints the final max / min (as determined by opcode)
if (opcode == 1) printf("The final maximum is %d\n",max);
else printf("The final minimum is %d\n",min);
return 0;
}
How to return array of elements from a void function in another c file. My assignment is to reverse the array in lab8.c and then use main.c to show the results I have no idea how to reverse the elements or print the results in main. Functions outside of my main cannot use printf or scanf
main.c
#include <stdio.h>
#include "lab8.h"
int main(void) {
int x[100];
int y[100];
int n = 0;
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the %i elements of the first array\n", count);
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Enter the %i elements of the second array\n", count);
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product of first array and second: %i\n", product);
printf("Enter the %i elements of the array\n", count);
for(i=0; i<count; i++){
scanf("%i", &n[i]);
}
reverse(n, count);
printf("Reverse of array 1: %i\n", n);
return(0);
}
lab8.c
#include <stdio.h>
#include "lab8.h"
int inner_product(int a[], int b[], int count){
int i;
int result = 0;
for( i=0; i<count; i++){
result = result + (a[i] * b[i]);
}
return result;
}
void reverse(int a[], int count){
int i, r, end = count - 1;
for(i=0; i<count/2; i++)
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
There is one mistake in your code, the for loop in the reverse() function has no braces and hence it's just executing r = a[i] count / 2 times.
void reverse(int a[], int count) {
int i, r, end = count - 1;
for (i = 0 ; i < count / 2 ; i++) {
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
}
and to print the result in main() just
reverse(x, count);
printf("Reverse of array 1: %i\n", n);
for (i = 0 ; i < count ; ++i)
printf("%d ", x[i]);
printf("\n");
also, do not ignore the return value of scanf() if it doesn't succeed at reading the values your program will invoke UNDEFINED BEHAVIOR, you should learn good practices right from the beginning.