LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2019, 07:04 AM   #1
sibelius
Member
 
Registered: Oct 2018
Location: Leura NSW Australia
Distribution: Linux Knoppix 8.1
Posts: 53

Rep: Reputation: 0
array of integer in C new problem


hi everybody
this time i am stuck on the following problem:
let's suppose i have an array[2,3,5,7] and a certain number n.
my goal is to create a new array in the following way.
the integer n divide array[0] till some condition is true; if false n start to divide array[1] till the same condition is true........ if false n divide array[3] (last component) till the same condition is true.

all the time the condition is not verify, before the cycle goes to the next step i need to count the number of division.
in the end in the new array I'd like to see integers which one represent the number of division while the condition impose is true.

thank you in advance for any suggestion.

my wrong pseudo code
Code:
for(i=0; i<Q; i++) {
k=0;
while(n%h[i]==0)
k=k+1;
printf("%d\n",k); }

Last edited by sibelius; 08-24-2019 at 07:04 AM. Reason: wrong code
 
Old 08-24-2019, 07:53 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,844

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
Perhaps something like this:
Code:
for(i=0; i<Q; i++) {
k=0;
tmp=h[i];
while(tmp%n==0) {
    k=k+1;
    tmp/=n;
}
printf("out[%d]=%d\n",i,k);
}
 
1 members found this post helpful.
Old 08-25-2019, 11:05 PM   #3
sibelius
Member
 
Registered: Oct 2018
Location: Leura NSW Australia
Distribution: Linux Knoppix 8.1
Posts: 53

Original Poster
Rep: Reputation: 0
Thank you, I 'll try your way.
Thanks again
 
Old 08-27-2019, 05:14 PM   #4
sibelius
Member
 
Registered: Oct 2018
Location: Leura NSW Australia
Distribution: Linux Knoppix 8.1
Posts: 53

Original Poster
Rep: Reputation: 0
thank you very much again for your help NevemTeve, I solve the problem and I post the solution.
Code:
for(i=0;i<n;i++)   {
k=0;
tmp=g[i];
while(n%tmp==0){
n=n/tmp;
k=k+1;         }

printf("%d\n",k);   }
I'm going also to post the entire code; the output of the code are: a list of prime numbers minor or equal to n, the difference between consecutive prime numbers,the total number of prime numbers in the range (2;n), the list of prime factor and the list of the exponents of the prime factor to rebuild n.
here the code
and sorry no time for good indentation
Code:
#include <stdio.h>
#define N 5000

int insert(int *v) {    
 	int i, n;
  	
  	printf("controlliamo se n e' >= 2 e in tal caso procediamo\n");
  	printf("2 e' l unico numero primo pari, per controllare se n e' primo basta vedere se nella colonna dei numeri primi generati\n");
  	printf("l ultimo numero coincide con n, se coincide il numero e' primo altrimenti non lo e'\n");
  	printf("controlliamo se n e' pari o dispari\n");
  	printf("troviamo i numeri primi <= di n con il Crivello di Eratostene\n");	
  	printf("attenzione se inserisci un numero con il punto o con la virgola il codice\n");
  	printf("va in loop e per fermarlo occorre premere ctrl+c e ricompilarlo per ripartire\n\n\n\n");
  	
  	while (n<2) {
  	printf("il numero deve essere >=2, per favore inseriscilo qui:");
  	scanf("%d", &n);
  	             }
  	
  		if (n%2 == 0)
  		printf("il numero e' positivo e pari\n");
  		else
  		printf("il numero e' positivo e dispari\n");
                printf("\n");

 /*generiamo un vettore di zeri*/
  	for(i=2; i<=n; i++)
  		v[i]=0;
   
  	return n;
                   }



void eratostene(int *v, int n) {
  	int i,j;
  	
 	for(i=2; i<=n; i++)
		if(v[i]==0)
    		         for(j=2*i; j<=n; j+=i) 
					    
    				v[j]=1;			//let's set v[j]=1, to mark all the multiple number ; that's the sieve 
    				                }
    				        
    				                
void print(int *v, int n) {
	int i,k;
	
	k=0;
   for(i=2; i<=n; i++) 
   if(v[i]==0) //if the number is prime
   k=k+1;  //count the number of prime numbers
   printf("\nnumeri primi compresi tra 2 e %d sono %d:\n\n",n,k);  

                             }
                          
 int primo (int *v, int *q,int n)		{//memorizzo i primi nel vettore q[i] ma contiene anche gli zeri
 			int i,j;
 			
 		 for(i=2; i<=n; i++)   
		 if(v[i]==0)
	                                                    
		 q[i]=i;  //vettore che contiene i numeri primi e gli zeri come distanze
		 
		                                  }

int terzo (int *q,int *z,int n)    {  //creo un nuovo vettore di primi senza gli zeri
                 int i,j,M,flag;
                 
                 
                 M=0; //dimensione nuovo vettore
                 
                 for(i=2; i<=n; i++) 
                 if(q[i]!=0)         {
			    flag=0;   
			    
			    
		for(j=0;j<=M ;j++)   
		if(z[j]==q[i])
			    flag=1;           

                                     
		if(flag==0)     
		z[M]=q[i];     
		M=M+1;         
				}//chisura ciclo if
                          printf("numeri primi memorizzati in un vettore\n");  //ho eliminato gli zeri da q nel vettore z
                                         for(i=0; i<M; i++) 
                                             printf("%d\t",z[i]);
                          
                                   }


                                
int quarto (int *z,int *w)    {  //trovo il vettore delle differenze tra due numeri primi successivi
                 int i,j,M;  
                 printf("\n\n");
                 printf("vettore differenze tra due primi successivi\n");
                 for(i=0; i<M; i++) {
                 w[i]=z[i+1]-z[i];  }
                 
                // printf("\nvettore delle distanze ossia intervalli aperti tra due primi successivi\n");
                 
                 for(i=0; i<M-1; i++)   {
                 printf("%d\t",w[i]); }

                                    }


                                


int quinto (int *z,int *f,int n) {    //funzione che memorizza i fattori di n inclusi gli zeri in un vettore f
                
                 int i,j,M,flag;
                 
            					     printf("\n");
                                                     printf("stampiamo i fattori primi di %d\n",n);
                                                     
                	                             for(i=0; i<M; i++) {
                	                             if(n%z[i]==0)
                	                             f[i]=z[i];
                                                                        } 
                                         }

    
int sesto (int *z,int *f,int *g,int n) {
                 int i,j,L,M,flag,k;
                 
                  L=0; //dimensione nuovo vettore
                 
                 for(i=0; i<n; i++) 
                 if(f[i]!=0)         {
			    flag=0;   
			    
		for(j=0;j<=L ;j++)   
		if(g[j]==f[i])
			    flag=1;           
                                     
		if(flag==0)     
		g[L]=f[i];     
		L=L+1;                //L e' la dimensione del vettore che contiene i fattori di n senza gli zeri
				}//chisura ciclo if
                         // printf("\nfattori primi memorizzati in un vettore\n");  //ho eliminato gli zeri da f
                                         for(i=0; i<L; i++) {
                                             printf("%d\n",g[i]);}

                                                                          }

int ottavo (int *g,int n) { //funzione che stampa gli esponenti dei fattori primi

                 int i,k,tmp;

                printf("esponenti dei fattori primi\n");
                            
		for(i=0;i<n;i++){
                k=0;
                tmp=g[i];
                while(n%tmp==0){
                n=n/tmp;
                k=k+1;         }

                           printf ("%d\n",k);
                               }

                                  }


int main() {

        
	int a[N],b[N],c[N],d[N],e[N],x[N],y[N],n; //a=v; b=q, c=z; d=w; e=f; g=x; h=y
	
	n=insert(a);
	eratostene(a,n);
	print(a,n);
	primo(a,b,n);
	terzo(b,c,n);
	quarto(c,d);
        quinto(c,e,n);
        sesto(c,e,x,n);
        ottavo(x,n);
  return 0; }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
[SOLVED] How to convert negative integer to byte array? Basel Programming 5 10-27-2010 12:35 PM
Convert 64 bit integer to char array in C++ syseeker Programming 2 06-27-2006 03:33 AM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
c: array of poiners(integer or double) jetfreggel Programming 3 12-29-2002 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration